class Dog
{
public:
class Animal
{
public:
Animal(Dog* dog)
{
m_Dog = dog;
m_Age = 1;
m_Name = dog->m_Info; // 可以访问宿主类对象
}
string m_Name;
int getAge()
{
return m_Age;
}
private:
Dog* m_Dog;
int m_Age;
};
public:
Dog() :m_Info("哮天犬"), m_Animal(this)
{
}
void showInfo()
{
cout << m_Animal.m_Name << endl;
//cout << m_Animal.m_Age << endl; /*不可访问*/
cout << m_Animal.getAge() << endl;
}
private:
string m_Info;
Animal m_Animal;
};
int main()
{
Dog dog;
dog.showInfo();
return 0;
}
输出:
哮天犬
1
内部类嵌套类可以访问外部宿主类的私有成员。
标签:测试,Age,Dog,C++,dog,嵌套,Animal,public From: https://www.cnblogs.com/huvjie/p/18389502