构造函数和析构函数问题
最新推荐文章于 2018-05-30 00:11:09 发布
#include <iostream> using namespace std; class A { public: A() { cout<<"A构造了"<<endl; } void Reset(); void SetData(int newx); void ShowData(); ~A() { cout<<"A析构了"<<endl; } private: int x; }; class B { public: B() { cout<<"B构造了"<<endl; } void Reset(); void SetData(A newy1,A newy2); void ShowData(); ~B() { cout<<"B析构了"<<endl; } private: A y1,y2; }; void A :: Reset() { x=0; } void A :: SetData(int newx) { x=newx; } void A ::ShowData() { cout<<"A类:x="<<x<<endl; } void B ::Reset() { y1.Reset(); y2.Reset(); } void B ::SetData(A newy1,A newy2) { y1=newy1; y2=newy2; } void B ::ShowData() { cout<<"B类嵌套"; y1.ShowData(); cout<<"B类嵌套"; y2.ShowData(); } void main() { A a; B b; a.SetData(100); b.SetData(a,a); a.ShowData(); b.ShowData(); a.Reset(); b.Reset(); a.ShowData(); b.ShowData(); } 结果中为什么会出现那么多个析构? 谁能解释下每句构造和析构是怎么来的吗?