Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > virtual inheritance

Reply
Thread Tools

virtual inheritance

 
 
sksjava
Guest
Posts: n/a
 
      02-11-2004
#include <iostream>
struct parent {
parent() : integer(0) { cout << "parent default\n"; }
parent(int i) : integer(i) { cout << "parent non-default\n"; }
int integer;
};

struct child1 : public parent {
child1() { cout << "child1 default\n"; }
child1(int i) : parent(i) { cout << "child1 non-default\n"; }
};

struct gchild : public child1 {
gchild(int i) : child1(i) { cout << "gchild non-default\n"; }
};

int main() {
gchild g(10);
cout << g.integer << endl;
}


The above code prints:
parent non-default
child1 non-default
gchild non-default
10


Modifying the line "struct child1 : public parent" to "struct child1 :
public virtual parent" will give the following result:
parent default
child1 non-default
gchild non-default
0


I am totally confused on what the compiler is doing. Thanks in advance.




 
Reply With Quote
 
 
 
 
Michael Mellor
Guest
Posts: n/a
 
      02-11-2004
sksjava wrote:
> #include <iostream>


using std::cout;
using std::endl;

> struct parent {
> parent() : integer(0) { cout << "parent default\n"; }
> parent(int i) : integer(i) { cout << "parent non-default\n"; }
> int integer;
> };
>
> struct child1 : public parent {
> child1() { cout << "child1 default\n"; }
> child1(int i) : parent(i) { cout << "child1 non-default\n"; }
> };
>
> struct gchild : public child1 {
> gchild(int i) : child1(i) { cout << "gchild non-default\n"; }
> };
>
> int main() {
> gchild g(10);
> cout << g.integer << endl;
> }
>
>
> The above code prints:
> parent non-default
> child1 non-default
> gchild non-default
> 10
>
>
> Modifying the line "struct child1 : public parent" to "struct child1 :
> public virtual parent" will give the following result:
> parent default
> child1 non-default
> gchild non-default
> 0
>
>
> I am totally confused on what the compiler is doing. Thanks in advance.
>

In virtual inheritence the most derived type constructs the virtually
inherited object(s). Therefore the following should print the required
result:
struct gchild : public child1 {
gchild(int i) : child1(i), parent(10)
{
cout << "gchild non-default\n";
}
};
If you had removed the parent's default ctor it wouldn't have compiled.

Take the following example:
struct child1 : virtual public parent {
child1() : parent(20) { cout << "child1 default\n"; }
};

struct child2 : virtual public parent {
child2() : parent(10) { cout << "child2 default\n"; }
};

struct gchild : public child1, public child2 {
gchild() { cout << "gchild non-default\n"; }
};
what value should parent.integer be? Hence you have to call an
appropriate parent ctor in all the derived types.

Mike
 
Reply With Quote
 
 
 
 
buildsoc
Guest
Posts: n/a
 
      02-12-2004
i think that the compiler produced some code to invoke parent's default
constructor( parent() ) that locate the position of parent subject in the
gchild object.
these codes were put into both of child constructors and do some job like
landing party.
so the parent(int i) was not invoked.


~ Let us linux ~


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
 
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
Virtual inheritace -- when one inheritance of the base is virtual andthe other isn't. pauldepstein@att.net C++ 1 03-14-2009 03:45 PM
virtual inheritance and virtual function. Ashwin C++ 2 08-01-2006 12:48 PM
mul. inheritance & overloading operator new/delete solved by virtual base inheritance? cppsks C++ 0 10-27-2004 07:49 PM
Should 'public virtual' always become 'private virtual'? & using private inheritance qazmlp C++ 19 02-04-2004 12:37 AM
Private access modifier and Inheritance (Inheritance implementation in Java) maxw_cc Java 1 12-21-2003 11:38 AM



Advertisments