#include <iostream>
using namespace std;
class A
{
private:
int n;
public:
A(int n)
{
this->n=n;
cout<<"A constructor"<<endl;
}
void show();
class B
{
public:
B()
{
cout<<"B constructor"<<endl;
}
void disp()
{
cout<<"B disp"<<endl;
}
};
B b;
};
void A::show()
{
cout<<n<<endl;
}
void main()
{
A a(1);
a.show();
a.b.disp();
}
//为什么B的构造函数先于A的构造函数调用
//还有 嵌套类和外围类是不是严格的不能通信
class A
{
private:
int n;
public:
A(int n)
{
this->n=n;
cout<<"A constructor"<<endl;
}
void show();
class B;
B b;
};
class A::B
{
public:
B()
{
cout<<"B constructor"<<endl;
}
void disp()
{
cout<<"B disp"<<endl;
}
};
void A::show()
{
cout<<n<<endl;
}
void main()
{
A a(1);
a.show();
a.b.disp();
}
// 这样为什么不可以
问题补充:
class A
{
private:
int n;
public:
A(int n)
{
this->n=n;
cout<<"A constructor"<<endl;
}
void show();
// class B;
class B
{
public:
B()
{
cout<<"B constructor"<<endl;
}
void disp()
{
cout<<"B disp"<<endl;
}
};
// B b;
};
void A::show()
{
cout<<n<<endl;
}
void main()
{
A a(1);
a.show();
// a.b.disp();
}
//明白了,关于B先于A完全是因为定义了一个B的对象,现在把这个对象删掉,则结果就没 有 B constructor 了 //不能定义未定义的类的对象,所以第二个错 多谢回答
2010-9-10 23:42
满意回答
原文链接: https://www.cnblogs.com/carbs/archive/2012/04/04/2431874.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/46034
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!