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); } }; I'm reading thinking in c++ chapter 11 default copy-constructor. For the above code, the author said: "The class WoCC has no copy-constructor, but its constructor will store a message in an internal string that can be printed out using print( ).This constructor is explicitly called in Composite’s constructor initializer list".
Why the WoCC constrcutor must be explicitly called in Composite's constructor?
WoCCdoes have a copy constructor. Since you didn't declare one, it gets an implicitly declared copy constructor.