Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!tut.cis.ohio-state.edu!ucsd!helios.ee.lbl.gov!pasteur!wenti!brianl From: brianl@wenti.Berkeley.EDU (Brian Lee) Newsgroups: comp.lang.c++ Subject: Re: Default constructor, unexpected call Keywords: default constructor, copy constructor Message-ID: <26580@pasteur.Berkeley.EDU> Date: 30 Jul 90 22:52:36 GMT Sender: news@pasteur.Berkeley.EDU Reply-To: brianl@wenti.Berkeley.EDU (Brian Lee) Lines: 65 X-Local-Date: 30 Jul 90 15:52:36 PDT In Article 8713, leo@atcmp.nl (Leo Willems) describes how the default constructor for a member class is sometimes called instead of the copy constructor. I am also using AT&T C++ Release 2.0, and a similar thing happens for base classes. I'm relatively new to this newsgroup, so please forgive me if this is old news. Consider the following program #include class X { public: X() { cout << "default X::X()\n"; } X(const X&) { cout << "copy X::X(const X&)\n"; }}; class Y : public X { public: Y() { cout << "default Y::Y()\n"; } // Y(const Y&) { cout << "copy Y::Y(const Y&)\n"; } }; class Z : public Y { public: Z() { cout << "default Z::Z()\n"; } Z(const Z&) { cout << "copy Z::Z(const Z&)\n"; } }; main() { cout << "\ndefault:\n"; Z k; cout << "\ncopy:\n"; Z l = k; } It produces the following output default: default X::X() default Y::Y() default Z::Z() copy: default X::X() default Y::Y() copy Z::Z(const Z&) Note that the default constructor for Y is called instead of a copy constructor being generated and that calling the default constructor for Y causes the default constructor for X to be called instead of the copy constructor for X. Assuming that the semantics of member-wise initialization extend to base class initialization, this is wrong. Try increasing the length of the inheritance chain and/or playing with different combinations of defined/undefined copy constructors. It appears that to be safe, one should define a default and copy constructor for all classes. Brian Lee U.C. Berkeley Cadgroup brianl@ic.Berkely.EDU