c++definition

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?


Solution

  • 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.