In accordance with C ++ 03 Standard 9.1 - 2
"A class definition introduces a class name in the area where it is defined, and hides any class, object, function or other declaration of that name in the scope (3.3)."
Thus, it is valid according to the standard.
Playing a bit with the sample code:
Here is the result:
prog.cpp: In function 'int main()': prog.cpp:6: error: 'int B::j' is private prog.cpp:13: error: within this context
The only difference between the C ++ struct and the class is that the default access specifier for the structure is public, and for the class it is private.
So, From the example above:
In this case, the compiler treats A as structure &
B as a class
As you can see, the compiler follows the quote from the standard, and the type with the definition is what the compiler recognizes by the type of declaration.
source share