![]() |
Invoking A Base class constructor
Consider the code fragment;
class A { public: A(){} A(int prm):mprm(prm){} int mprm; }; class B:public A { public: B(){} B(int prm):A(prm){} }; class C:public B//,public virtual A public: c(){} C(int prm):A(prm){} }; int main() { C c(3); } well my compiler says that 'A' is not a base class of 'C' so i assume you can only initialize your immediate base class(why?),but if i remove the comment and virtually inherit from 'A' the code runs ok, but i feel like i am creating two A objects (default A constructor called) which is not the intended behavior, is that true?Does virtual inheritance guarantee only one 'A' is created? |
Re: Invoking A Base class constructor
hurcan solter wrote:
> Consider the code fragment; > class A > { > public: > A(){} > A(int prm):mprm(prm){} > int mprm; > }; > class B:public A > { > public: > B(){} > B(int prm):A(prm){} > > }; > class C:public B//,public virtual A > public: > c(){} > C(int prm):A(prm){} > > > }; > int main() > { > > C c(3); > } > > well my compiler says that 'A' is not a base class of 'C' so i assume > you can only initialize your immediate base class(why?), Since an object is initialized only once, C cannot initialize A, because B already does that. You don't need that anyway. > but if i remove the comment and virtually inherit from 'A' the code runs > ok, That's because with virtual inheritance, B can't initialize A. Think of four classes building up the diamond structure: class A {}; class B : virtual public A {}; class C : virtual public A {}; class D: public B, public C {}; Now since B and C both share one A, they can't both initialize it. Therefore, D gets to initialize the A part. |
Re: Invoking A Base class constructor
"hurcan solter" <hsolter@gmail.com> wrote in message
news:1168213333.702476.325670@42g2000cwt.googlegro ups.com... > Consider the code fragment; > class A > { > public: > A(){} > A(int prm):mprm(prm){} > int mprm; > }; > class B:public A > { > public: > B(){} > B(int prm):A(prm){} > > }; > class C:public B//,public virtual A > public: > c(){} > C(int prm):A(prm){} > > > }; > int main() > { > > C c(3); > } > > well my compiler says that 'A' is not a base class of 'C' so i assume > you can only initialize > your immediate base class(why?),but if i remove the comment and > virtually inherit from 'A' the code runs ok, but i feel like i am > creating two A objects (default A constructor called) which is not the > intended behavior, is that true?Does virtual inheritance guarantee only > one 'A' is created? Class C is inherited from class B, so it contains a class B. Class B is inhertied from class A, so it contains a class A. In the C constructor, you need to initialize class B, which will initialize class A. C(int prm): B(prm) {} So your final class C will have one instance of B and one instance of A (if they're even called instances in this case, I think they are). |
Re: Invoking A Base class constructor
hurcan solter wrote: > A(){} In the case "mprm" is undefined. Write " A():mprm(0){} " > well my compiler says that 'A' is not a base class of 'C' so i assume > you can only initialize your immediate base class(why?), Because immediate base class already initialize all other base classes. Each base class does not know any about own derived, so do init. Do you want to initialize twice? Or do you want to know all base classes of the creating class? > Does virtual inheritance guarantee only one 'A' is created? Virtual inheritance guarantees, that class A will be initialized by constructor of real class of creating object, not by constructor of any its base classes _where_ class A declared as virtual too. It is similar to virtual members, which are guarantee, that compiler will call member of real class of object, not member of class of pointer or reference. Virtual inheritance turn constructor of class A as virtual member, so in each class with the virual base, you must write concrete ctor of virual base, but compiler will call concrete ctor, declared only in ctor of real class of creating object. It can be more questions here, but virtual inheritance is enough rare used, and in most cases (especially for novice) the kind of inheritance can be replaced by "adapter" design pattern. Note, that ordinary inheritance in most cases can be replaced by composition too and the last is better. It is seems to me, that virtual inheritance is "bad" only as "inheritance", not as "virtual" or "multiple". |
| All times are GMT. The time now is 06:28 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.