【发布时间】:2015-04-12 03:32:00
【问题描述】:
#include<iostream>
using namespace std;
class Test
{
private:
int x;
public:
Test(int i)
{
cout<<"Conversion constructor called..."<<endl;
x = i;
}
~Test()
{
cout<<"Destructor called..."<<endl;
}
void show()
{
cout<<" x = "<<x<<endl;
}
};
int main()
{
Test t(20);
t.show();
t = 30;
t.show();
return 0;
}
输出:
Conversion constructor called...
x = 20
Conversion constructor called...
Destructor called...
x = 30
Destructor called...
当我在做 t = 30 为什么它调用构造函数和析构函数? 请解释一下。非常感谢。
【问题讨论】:
标签: c++ constructor assignment-operator