Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!dlogics!dsa From: dsa@dlogics.COM (David Angulo) Newsgroups: comp.lang.c++ Subject: Re: Correction to Re: Default constructor, unexpected call Summary: buggy code Message-ID: <615@dlogics.COM> Date: 7 Aug 90 18:15:59 GMT References: <26655@pasteur.Berkeley.EDU> <643@atcmpe.atcmp.nl> Organization: Datalogics Inc., Chicago Lines: 97 In article <643@atcmpe.atcmp.nl>, leo@atcmp.nl (Leo Willems) writes: > My question is simple: why does the following program generate a > call to the default constructor from class A for variable 'copd'? > > (If this is not a bug in 2.1 (2.0 as well) try switching the > declarations of a and b in class C; then A::A() is not called: if the > previous one is NOT a bug then this one is!) > > > ============ > > #include > > class A{ > public: > A() { ida = 0; puts("default constructor A()"); } > A(int i) {ida = i; puts("constructor A(int)"); } > private: > int ida; > }; > > class B{ > public: > B() { idb = 0; puts("default constructor B()"); } > B(B& b) { idb = b.idb; puts("copy constructor B(B&)"); } > private: > int idb; > }; > > class C{ > public: > C(){ puts("default constructor C()");} > protected: > A a; //switch these: bug(?) gone > B b; // if not a bug: A::A() gone!!!! > }; > > main() > { > puts(" first a 'default' constructor:"); > C defd; > > puts("\n now the copy constructor: "); > > C copd(defd); //suspicious A::A() class here ^^^^ Well, I don't know how this compiled because you don't have anything other than a default constructor for class C. This shouldn't have compiled! > } > I hate this stupid program This is really stupid Who wrote this junk? -- David S. Angulo (312) 266-3134 Datalogics Internet: dsa@dlogics.com 441 W. Huron UUCP: ..!uunet!dlogics!dsa Chicago, Il. 60610 FAX: (312) 266-4473