Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should C++ permit a variable name and a class name to be the same? [closed]

#include <iostream>
#include <vector>   

using namespace std; 

typedef int UINT4;

class Hack
{};

Hack& operator <(Hack& a , Hack& b) 
{
    std::cerr << "1";  
    return a; 
}

Hack& operator >(Hack& a, Hack& b)
{
    std::cerr <<  "2";    
    return a; 
}

int main()
{
    Hack vector; // It SHOULD be OK!?
    Hack UINT4; // It SHOULD be OK!?
    Hack v;
    Hack Hack; // It SHOULD be OK!?
    Hack = v; // It SHOULD be OK!?

    // Please stop to think for a moment: What will the following output?
    vector<UINT4> v; 

    // Oh, my god! The whole world goes mad!

    return 0; 
} 
like image 217
xmllmx Avatar asked Dec 17 '10 07:12

xmllmx


People also ask

Can a variable and class have same name?

Question : Can we have a variable name as one of the predefined class name in our program? Answer : Yes we can have it.

Can the object name be same as the class in C++?

If you happen to use the same name for both a class/struct A and a variable/function A you have to use the struct / class keyword, because the compiler interprets all following occurrences of A as the variable/function and not the struct/class.

Can object name be same as class name?

We can have a method name same as a class name in Java but it is not a good practice to do so. This concept can be clear through example rather than explanations. In the below example, a default constructor is called when an object is created and a method with the same name is called using obj. Main().

Which identifier has the same name as a class?

Answer: A constructor is a special kind of subroutine in a class. It has the same name as the name of the class, and it has no return type, not even void. A constructor is called with the new operator in order to create a new object.


2 Answers

Ever heard of the following? Please look close at the parameter types :)

int stat(const char *path, struct stat *buf);

What you have shown in your question really isn't too dramatic. You declare a variable name in a local scope, but the type names in your program are in global scope. I will show you some real pervert thing now:

int nooo = 0;
int main() {
   int nooo = 0;
}

W00t? Why does C++ allow us to create two variables of the same name!?!

Alright, I was kidding. I will now introduce you into the real dark sides of messing with duplicate names. Consider what C++ allows us!

struct A {
  int A; // noes!
};

A A; // another A!

In essence, this one is all for C compatibility :)

like image 145
Johannes Schaub - litb Avatar answered Sep 23 '22 23:09

Johannes Schaub - litb


You're asking the question from the point of view or someone who very well knows that he is using a class name as a variable identifier : obviously, this isn't a best practice.

Now, let's take it the other way around : suppose you have no idea that there is a weird typedef int i; in some obscure header file you are including. Would you expect it to break the following innocent code ?

int i = 0;
like image 6
icecrime Avatar answered Sep 21 '22 23:09

icecrime