Path: utzoo!attcan!uunet!tektronix!tekcrl!soiffer From: soiffer@tekcrl.CRL.TEK.COM (Neil Soiffer) Newsgroups: comp.lang.c++ Subject: inheriting constructors Message-ID: <3447@tekcrl.CRL.TEK.COM> Date: 8 Jan 89 22:50:05 GMT Reply-To: soiffer@tekcrl.TEK.COM (Neil Soiffer) Organization: Tektronix, Inc., Beaverton, OR. Lines: 112 I could find nothing in "the book" or subsequent papers on whether constructors are inherited by a derived type. Normal functions are inherited, but in order for a constructor to be inherited, the compiler needs to recognize that the name of the constructor has changed to be the name of the derived class. All of the arguments that apply to why it is (sometimes) useful to inherit a normal function apply to why it is (sometimes) useful to inherit a constructor. In particular, when the derived type merely (re)defines some virtual functions, it is annoying (and error prone) to have to define constructors for the derived type that are identical to the base type's constructors. Because I could find nothing concerning the legality of inheriting constructors, I tried some examples with cfront 1.2.1 and g++ (v 1.27?). [Can you guess what the answer is -- you are almost certainly wrong :-)] Below are four very simple types: B and D (derived from B) have a single function "f" defined along with the constructor. VirtualB and VirtualD (derived from VirtualB) differ from B and D in that their single function "vf" is a virtual function. The main program tries to construct objects of type D, D*, virtualD, and virtualD*. Another behavior is demonstrated by a class hierarchy similar to "Virtual{B,D}" called Other{B,D,DD}. This hierarchy adds a third class adds a constructor to OtherD and a third class OtherDD that is derived from OtherD. Cfront allows the objects of type D and D*, but complains about constructors for virtualD and virtualD*. I believe that I had one example where cfront would allow a virtualD* but not a virtualD, but I can't seem to reconstruct that example anymore. In the "Other" example, cfront complains about the class definition of OtherDD "struct OtherDD needs a constructor". It also gives the error message argument 1 of type char * expected for OtherD::OtherD() g++ complains about the constructors for all of the objects, although the error messages differ for the malloc'd types: test.c:51: type `VirtualD' must have constructor to take parameter list test.c:54: structure has no method named `VirtualD' test.c:57: type `D' must have constructor to take parameter list test.c:60: structure has no method named `D' g++ does not complain about the "Other" example. I strongly feel that constructors should be inherited. Is this question answered anywhere? Is there a good reason why they shouldn't be inherited? What does cfront 2.0 do? Below is the test program: ----------------------------------------------- #include class B { public: B(int n=5) {i=n;}; void f() {printf("f of B\n");}; protected: int i; }; class D: public B { public: void f() {printf("f of D\n");}; }; class VirtualB { public: VirtualB(int n=5) {i=n;}; virtual void vf() {printf("vf of VirtualB\n");}; protected: int i; }; class VirtualD: public VirtualB { public: virtual void vf() {printf("vf of VirtualD\n");}; }; class OtherB { public: OtherB(int n=5) {i=n;}; virtual void vf() {printf("vf of OtherB\n");}; protected: int i; }; class OtherD: public OtherB { public: OtherD(char *s) {i = atoi(s);}; // this is the difference from Virtual above virtual void vf() {printf("vf of OtherD\n");}; }; class OtherDD: public OtherD { public: virtual void vf() {printf("vf of OtherDD\n");}; }; main() { VirtualD *vd_obj_ptr = new VirtualD(3); vd_obj_ptr->vf(); VirtualD vd_obj = VirtualD(3); vd_obj.vf(); D *d_obj_ptr = new D(3); d_obj_ptr->f(); D d_obj = D(3); d_obj.f(); }