class WithCC { // With copy-constructor
public:
// Explicit default constructor required:
WithCC() {}
WithCC(const WithCC&) {
cout << "WithCC(WithCC&)" << endl;
}
};
class WoCC { // Without copy-constructor
string id;
public:
WoCC(const string& ident = "") : id(ident) {}
void print(const string& msg = "") const {
if(msg.size() != 0) cout << msg << ": ";
cout << id << endl;
}
};
class Composite {
WithCC withcc; // Embedded objects
WoCC wocc;
public:
Composite() : wocc("Composite()") {}
void print(const string& msg = "") const {
wocc.print(msg);
}
};
我正在阅读思考C++第11章默认的复制构造函数。 对于上面的代码,作者说:“类WoCC
没有复制构造函数,但其构造函数将存储消息在一个内部字符串,可以打印出使用 print()
。此构造函数明确调用Composite’s
构造函数初始值设定项列表”。了解默认的构造函数C++
为什么WoCC
constrcutor必须在Composite
的构造函数中明确调用?
'WoCC'没有拷贝构造函数。既然你没有声明一个,它会得到一个*隐式声明的*拷贝构造函数。 – aschepler 2013-02-23 18:27:55