Today's whim, what happens if the class name is the same as the object identifier declared by this class, and then it's tested. As follows:
#include <iostream>using namespace Std;class a{public:a () {cout << "a~~~~~" << Endl;}}; int main (void) {a a;return 0;}
Normal compilation passes, and the result is normal. Think about this result is expected, because in the main function body in the beginning of the first encountered a, because the main local domain does not have a declaration, so continue to the outside of the global domain, then find a declaration, and resolve a is a class, and then encountered a second A, At this point A is an object of type A, and a in the local domain after this declaration has hidden a in the global domain, if you continue to write a B; There will be an error, as follows:
#include <iostream>using namespace Std;class a{public:a () {cout << "a~~~~~" << Endl;}}; int main (void) {a a;a b;return 0;}
This is definitely not going to compile. If you want to access the global that a can only use the domain operator (:: A), as follows:
#include <iostream>using namespace Std;class a{public:a () {cout << "a~~~~~" << Endl;}}; int main (void) {a a;//a b;::a B;return 0;}
C + +, what happens when the class name and object name are the same?