先看一种情况:注意橘色的部分,是用的类名定义
#include <iostream> using namespace std; class Hen { public: void display() { cout<<"this is Hen display()"<<endl; } class Nest { public: int Egg; void display() { cout<<"this is Nest display()"<<endl; } }; }; int main() { Hen hen; hen.display(); Hen::Nest nest; nest.display(); return 0; }
另外一种情况:
#include <iostream> using namespace std; class Nest { public: int Egg; void display() { cout<<"this is Nest display()"<<endl; } }; class Hen { public: void display() { cout<<"this is Hen display()"<<endl; } Nest nest; }; int main() { Hen hen; hen.display(); hen.nest.display(); return 0; }