65,210
社区成员
发帖
与我相关
我的任务
分享#include <iostream>
#include <string>
using namespace std;
class A{
public:
A(int a)
:x(a)
{}
~A()
{
cout<<"A destroy construction.\n"<<endl;
}
private:
int x;
};
class B{
public:
B(int a,int b)
:Aa(a),y(b)
{}
~B()
{}
private:
A Aa;
int y;
};
int main()
{
B b(2,3);
return 0;
}
在没有更好的解释了吗#include <iostream>
#include <string>
using namespace std;
class A{
public:
A(int a)
:x(a)
{}
~A()
{
cout<<"A destroy construction.\n"<<endl;
}
private:
int x;
};
class B{
public:
B(int a,int b)
:Aa(a),y(b)
{}
~B()
{ this->Aa.~A(); }//调用A的析构函数
private:
A Aa;
int y;
};
int main()
{
B b(2,3);
return 0;
}
这段代码或许会说明问题吧, 自定义的~B()中调用了一次A的析构函数,B类合成的析构函数又调用了一次,与输出的结果相一致。
我觉的这样的问题必须弄明白,否则程序大了的话,一个小的错误或许就会让你检查半天
class trivial{
int a, b, c;
};
trivial是不会有constructor也不会有destructor的
他的开销和c的struct一模一样
如果有错的话希望各位可以指出来,我这是抛砖引玉
