By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,997 Members | 1,260 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,997 IT Pros & Developers. It's quick & easy.

Invoking A Base class constructor

P: n/a
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?

Jan 7 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.

Jan 8 '07 #2

P: n/a
"hurcan solter" <hs*****@gmail.comwrote in message
news:11**********************@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).
Jan 8 '07 #3

P: n/a

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".

Jan 12 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.