C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Virtual base class and order of CTOR

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Thu Feb 23, 2006 7:06 pm    Post subject: Virtual base class and order of CTOR Reply with quote



#include<iostream>

using namespace std;

class A
{
public:
A()
{ cout<<"I am in A"<<endl; }
};
class A1
{
public:
A1() { cout<<"I am in A1"<<endl;}

};

class B: public A, public A1
{
public:
B()
{ cout<<"I am in B"<<endl; }
};
class C: public A, virtual public A1
{
public:
C()
{ cout<<"I am in C"<<endl; }
};

int main(int argc, char **argv)
{
cout<<"Lets make B"<<endl;
B b1;
cout<<"Lets make C"<<endl;
C c1;
return 0;
}


Output of executable

Lets make B
I am in A
I am in A1
I am in B
Lets make C
I am in A1
I am in A
I am in C

Question.
1. Why did virtual base class get higher priority in construction?
2. Is this behavior accepted by standard?
3. I know that virtual inheritance is useful when two copies of base
class exist in derived class. (via diamond shape inheritance) However,
this behavior makes me think that there is something more in virtual
inheritance, which I have not yet understood. Can some one please
provide some links or book names, which deal with this topic in depth?



Regards
Sushrut


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Thomas Maeder
Guest





PostPosted: Fri Feb 24, 2006 3:06 am    Post subject: Re: Virtual base class and order of CTOR Reply with quote



bestbrain (AT) gmail (DOT) com writes:

Quote:
Question.
1. Why did virtual base class get higher priority in construction?

Because that's what the Standard requires.


Quote:
2. Is this behavior accepted by standard?

Yes.


Quote:
3. I know that virtual inheritance is useful when two copies of
base class exist in derived class. (via diamond shape inheritance)

.... and if there should only be one sub-object of that class in the
derived class.


Quote:
However, this behavior makes me think that there is something more
in virtual inheritance, which I have not yet understood. Can some
one please provide some links or book names, which deal with this
topic in depth?

Every good C++ text book. Have a look at the reviews at
http://www.accu.org/ to find one.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
kanze
Guest





PostPosted: Fri Feb 24, 2006 11:06 am    Post subject: Re: Virtual base class and order of CTOR Reply with quote



Thomas Maeder wrote:
Quote:
bestbrain (AT) gmail (DOT) com writes:

Question.
1. Why did virtual base class get higher priority in
construction?

Because that's what the Standard requires.

The real question is what other rule could be made to work.

In fact, the virtual base class is always constructed by the
most derived class, before is constructs any non-virtual bases.
The virtual base must be constructed before any class which
derives from it, and in complex hierarchies, it's not always
apparent who should construct it, unless the most derived class
does.

Note that this has an additional side effect: if the constructor
for the virtual base has parameters, it is the most derived
class which provides them.

Quote:
2. Is this behavior accepted by standard?

Yes.

More than accepted, it is required.

Quote:
3. I know that virtual inheritance is useful when two
copies of base class exist in derived class. (via diamond
shape inheritance)

... and if there should only be one sub-object of that class in the
derived class.

In fact, just the opposite of what he actually said: virtual
inheritance is useful when there shouldn't be two copies of the
base class, even though two (or more) subclasses derive from it.

Quote:
However, this behavior makes me think that there is
something more in virtual inheritance, which I have not yet
understood. Can some one please provide some links or book
names, which deal with this topic in depth?

Every good C++ text book. Have a look at the reviews at
http://www.accu.org/ to find one.

I wonder about that. I think it is a topic that is often
ignored.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Guest






PostPosted: Sat Feb 25, 2006 12:06 pm    Post subject: Re: Virtual base class and order of CTOR Reply with quote

Quote:
1. Why did virtual base class get higher priority in construction?
This is as per standards. And this is a standard because there is a

specific reason for this.
We all are aware of virtual inheritance i.e. diamond inheritance.
Consider an example :
class B {};
class B1 {}:piblic virtual B {};
class B2: public virtual B {};
Have you ever thought how does compiler assure that we are referering
to same object from class B1 and class B2. This is done by inserting
virtual base pointer in class B1 and class B2 which will be pointing to
same object of class B. Constructor of class B1 and class B2 is
responsible for intializing virtual base pointer in their
object.Therefore, before the constructor of class B1 and class B2 gets
called, construction of class B must have been called first to allocate
memory to class B object so that virtual base pointer should points to
valid object.

Quote:
2. Is this behavior accepted by standard?
Yes.


Quote:
3. I know that virtual inheritance is useful when two copies of base
class exist in derived class. (via diamond shape inheritance) However,
this behavior makes me think that there is something more in virtual
inheritance, which I have not yet understood. Can some one please
provide some links or book names, which deal with this topic in depth?
Virtual inheritance is best suitable for diamond shape inheritance. But

there could be an overheads involved with virtual keyword when we
deals with class inheritance.

class A
{
};
class B: public A
{
};

cout << sizeof(B); // 1

class A
{
};
class B: public virtual A
{
};
cout << sizeof(B); // 4 This is because of sizeof(virtual base pointer)
is 4. This has been overhead as virtual keyword is not solving our
purpose. Use of virtual keyword causes an insertion of virtual base
pointer so be careful while using virtual keyword.

Regards
Dinesh


bestbrain (AT) gmail (DOT) com wrote:
Quote:
#include<iostream

using namespace std;

class A
{
public:
A()
{ cout<<"I am in A"<<endl; }
};
class A1
{
public:
A1() { cout<<"I am in A1"<<endl;}

};

class B: public A, public A1
{
public:
B()
{ cout<<"I am in B"<<endl; }
};
class C: public A, virtual public A1
{
public:
C()
{ cout<<"I am in C"<<endl; }
};

int main(int argc, char **argv)
{
cout<<"Lets make B"<<endl;
B b1;
cout<<"Lets make C"<<endl;
C c1;
return 0;
}


Output of executable

Lets make B
I am in A
I am in A1
I am in B
Lets make C
I am in A1
I am in A
I am in C

Question.
1. Why did virtual base class get higher priority in construction?
2. Is this behavior accepted by standard?
3. I know that virtual inheritance is useful when two copies of base
class exist in derived class. (via diamond shape inheritance) However,
this behavior makes me think that there is something more in virtual
inheritance, which I have not yet understood. Can some one please
provide some links or book names, which deal with this topic in depth?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.