构造函数和析构函数问题
来源:互联网 发布:第四方支付 知乎 编辑:程序博客网 时间:2024/04/29 05:21
#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();} 结果中为什么会出现那么多个析构?谁能解释下每句构造和析构是怎么来的吗?
#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();} 结果中为什么会出现那么多个析构?谁能解释下每句构造和析构是怎么来的吗?
- 构造函数和析构函数问题
- 构造函数,析构函数和虚函数相关问题
- C++的构造函数 和 析构函数的问题
- 构造函数和析构函数的一些问题
- 构造函数和析构函数调用问题
- 构造函数和析构函数的调用问题
- 构造函数和析构函数的小问题
- c++构造函数重载和析构函数问题
- 构造函数,拷贝构造函数和析构函数
- 构造函数、拷贝构造函数和析构函数
- 构造函数和析构函数的重载和虚函数的问题
- (1)为什么构造函数的名字用类名?(2)构造函数返回值问题;(3)析构函数和构造函数定义为虚函数;
- 构造函数和析构函数
- 构造函数和析构函数
- 构造函数和析构函数
- 构造函数和析构函数
- 构造函数和析构函数
- 关于构造函数和析构函数
- 浅谈C++多态性
- 动画
- Python 自学资料
- IOS控件TableView下拉刷新
- 十个iOS面试问题【2年工作经验】
- 构造函数和析构函数问题
- jquery获得select option的值 和对select option的操作
- 星号图
- Documentation/sh/register-banks.txt
- 关于情感
- java的构造器Constructor的理解
- 定义类的属性
- Using TFS 2012 and SCRUM for Iteration Capacity Planning for Software Development
- windows下webRTC编译过程