Invoking A Base class constructor

Discussion in 'C++' started by hurcan solter, Jan 7, 2007.

  1. 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?
     
    hurcan solter, Jan 7, 2007
    #1
    1. Advertising

  2. hurcan solter

    Rolf Magnus Guest

    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.
     
    Rolf Magnus, Jan 8, 2007
    #2
    1. Advertising

  3. hurcan solter

    Jim Langston Guest

    "hurcan solter" <> wrote in message
    news:...
    > 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).
     
    Jim Langston, Jan 8, 2007
    #3
  4. hurcan solter

    Grizlyk Guest

    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".
     
    Grizlyk, Jan 12, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dominique
    Replies:
    7
    Views:
    403
    amit gulati
    Nov 17, 2003
  2. mrstephengross
    Replies:
    5
    Views:
    618
    Larry I Smith
    May 18, 2005
  3. ali
    Replies:
    4
    Views:
    607
    David Harmon
    Mar 5, 2007
  4. Rahul
    Replies:
    16
    Views:
    1,237
    terminator
    Nov 7, 2007
  5. Lilith

    Invoking base constructor

    Lilith, Dec 12, 2007, in forum: C++
    Replies:
    1
    Views:
    339
    Victor Bazarov
    Dec 12, 2007
Loading...

Share This Page