c++初始化列表
初始化列表及拷贝构造函数
#include <iostream>
using namespace std;
class Str
{
public:
Str(int x):
val(x)
{
cout<<"construct is called"<<endl;
}
Str(const Str& x):
val(x.val)
{
cout<<"copy construct is called"<<endl;
}
~Str()
{
cout<<"Destructor is called"<<endl;
}
private:
int val;
};
int main()
{
Str m(3);
Str m2(m);
}
输出结果
construct is called
copy construct is called
Destructor is called
Destructor is called