Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Invoking A Base class constructor

Reply
Thread Tools

Invoking A Base class constructor

 
 
hurcan solter
Guest
Posts: n/a
 
      01-07-2007
Consider the code fragment;
class A
{
public:
A(){}
A(int prm):mprm(prm){}
int mprm;
};
class Bublic A
{
public:
B(){}
B(int prm):A(prm){}

};
class Cublic 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?

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      01-08-2007
hurcan solter wrote:

> Consider the code fragment;
> class A
> {
> public:
> A(){}
> A(int prm):mprm(prm){}
> int mprm;
> };
> class Bublic A
> {
> public:
> B(){}
> B(int prm):A(prm){}
>
> };
> class Cublic 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.

 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      01-08-2007
"hurcan solter" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Consider the code fragment;
> class A
> {
> public:
> A(){}
> A(int prm):mprm(prm){}
> int mprm;
> };
> class Bublic A
> {
> public:
> B(){}
> B(int prm):A(prm){}
>
> };
> class Cublic 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).


 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      01-12-2007

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Invoking base constructor Lilith C++ 1 12-12-2007 09:57 PM
call base class constructor from derived class constructor Rahul C++ 16 11-07-2007 03:40 PM
Calling base class constructor from derived class Copy constructor ali C++ 4 03-05-2007 09:15 AM
Invoking templatized base class constructor from templatized derived class constructor mrstephengross C++ 5 05-18-2005 07:12 PM
Invoking a base class constructor Dominique C++ 7 11-17-2003 03:37 PM



Advertisments