Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!apple!bionet!agate!ucbvax!hplabs!hp-pcd!hplsla!jima From: jima@hplsla.HP.COM (Jim Adcock) Newsgroups: comp.lang.c++ Subject: Re: constructors for virtual base classes Message-ID: <6590185@hplsla.HP.COM> Date: 6 Jul 89 23:35:53 GMT References: <1429@masscomp.UUCP> Organization: HP Lake Stevens, WA Lines: 50 //It seems to me that the constructor for "common" will be called twice. //What am I doing wrong? - Keith //Maybe nothing -- I made the following slight mods to your example and //under my compiler common() only gets called once. Is this what you //wanted? See Lippman pages 360-370. #include class common { public: common () {printf("common ");} }; class virtual_1 : virtual public common { public: virtual_1 () : common () {printf("virtual_1 ");} }; class virtual_2 : virtual public common { public: virtual_2 () : common () {printf("virtual_2 ");} }; class both : public virtual_1, public virtual_2 { public: both () : virtual_1 (), virtual_2 () {printf("both ");} }; main() { common c; printf("\n"); virtual_1 v1; printf("\n"); virtual_2 v2; printf("\n"); both b; printf("\n"); } /************* gives this output ***************/ common common virtual_1 common virtual_2 common virtual_1 virtual_2 both