sharptooth
sharptooth

Reputation: 170439

Why is ClassName ClassName variable definition in C++ compiling and working properly?

Say I have a class definition:

class CustomClass {
    int member;
};

Why is the following variable definition compiling and working correctly:

CustomClass CustomClass; // the variable is properly constructed

Should't this confuse the compiler and cause it to indicate an error?

Upvotes: 2

Views: 1511

Answers (4)

Richard Corden
Richard Corden

Reputation: 21721

This answer indirectly states the main reason why it is possible to do this in C++. It comes down to backwards compatibility with C. In C types had a different namespace and so it was possible to declare a type and object, or a type and function with the same name.

In order to be compatible with C, C++ added some special rules to allow an object or a function to hide the declaration of a type.

Upvotes: 0

Abhay
Abhay

Reputation: 7180

I think its compiler magic thats making it work. I agree with you that this ideally should be a compiler error (atleast confuses me).

If you try something like

#include <iostream>
class Test { public: int member; };
Test Test;   // comaeu warns 'expression has no effect!'
Test.member = 10; // dosen't compile!

int main(){
  Test Test;   
  Test.member = 10; // compiles fine after global 'Test's are commented!!
  std::cout<<Test.member<<std::endl;
  return 0;
}

The use of 'Test.member' at global scope will not compile, but the same works inside 'main()' after both the global 'Test's are commented.

C++ has enough complexities to excite programmers, how about compilers too contributing :-) ?

Upvotes: 0

Don McCaughey
Don McCaughey

Reputation: 9972

Class names and variable names occupy two separate namespaces. The compiler is able to figure out that the first CustomClass is a type and the second CustomClass is a variable name.

Upvotes: 10

Lucas
Lucas

Reputation: 3119

The requested doubt is not necessarily about the case sensitive mode of C++ , it's the variable declaration that has the same name as the class defined above. I'm thinking that your c++ compiler is smart enough to deduce the type of the token that it's parsing ..

Upvotes: 1

Related Questions