Connecting Tech Pros Worldwide Help | Site Map

about virtual inherit

  #1  
Old July 19th, 2005, 06:28 PM
DarkSpy
Guest
 
Posts: n/a
code here:

struct A
{
int _i, _j;
A() { cout<<"call default ctor"<<endl; }
A(int i, int j) : _i(i), _j(j) { cout<<"call paramater ctor"<<endl;
}
};

struct B : virtual A
{
B(int i, int j) : A(i, j){}
};

struct C : virtual B // or not virtual, just: public B
{
C(int i, int j) : B(i, j) {}
};

main()
{
C c(1, 2);
}
the result is just call default constructor A() not called A(int,
int), why ? if i wanna init the _i, _j in class A with class C, how to
do this ? (not using other help functions.).
why the inherit class from virtual inherit class will not call the
paramater's constructor ? who can tell me why ?
thanks anyway
  #2  
Old July 19th, 2005, 06:28 PM
John Carson
Guest
 
Posts: n/a

re: about virtual inherit


"DarkSpy" <coneos@21cn.com> wrote in message
news:aacb0456.0309122348.ad8d881@posting.google.co m[color=blue]
> code here:
>
> struct A
> {
> int _i, _j;
> A() { cout<<"call default ctor"<<endl; }
> A(int i, int j) : _i(i), _j(j) { cout<<"call paramater ctor"<<endl;
> }
> };
>
> struct B : virtual A
> {
> B(int i, int j) : A(i, j){}
> };
>
> struct C : virtual B // or not virtual, just: public B
> {
> C(int i, int j) : B(i, j) {}
> };
>
> main()
> {
> C c(1, 2);
> }
> the result is just call default constructor A() not called A(int,
> int), why ? if i wanna init the _i, _j in class A with class C, how to
> do this ? (not using other help functions.).
> why the inherit class from virtual inherit class will not call the
> paramater's constructor ? who can tell me why ?
> thanks anyway[/color]


I believe that the answer is as follows. A is a virtual base of C,
regardless of whether you use virtual inheritance to go from B to C. The
most derived class is responsible for calling the constructor of a virtual
base. Thus C must call the constructor of A, it can't have B do it. Thus you
need:

struct C : virtual B
{
C(int i, int j) : A(i,j), B(i, j) {}
};


--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)


  #3  
Old July 19th, 2005, 06:28 PM
Kevin Goodsell
Guest
 
Posts: n/a

re: about virtual inherit


DarkSpy wrote:
[color=blue]
> code here:
>[/color]

I don't know the answer to your actual question, but I have a few
comments on your code.
[color=blue]
> struct A
> {
> int _i, _j;[/color]

While (I think) this is technically fine, I strongly recommend never
using identifiers that begin with an underscore. There are complicated
ruled governing when such identifiers are and are not allowed. It's much
easier and safer to avoid them completely.

<snip>
[color=blue]
>
> main()[/color]

This must be

int main()

There is no 'implicit int' rule in C++, and there hasn't been for many
years.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

  #4  
Old July 19th, 2005, 06:29 PM
Jonathan Mcdougall
Guest
 
Posts: n/a

re: about virtual inherit


>> main()[color=blue]
>
>This must be
>
>int main()
>
>There is no 'implicit int' rule in C++, and there hasn't been for many
>years.[/color]

There never was.


Jonathan

  #5  
Old July 19th, 2005, 06:29 PM
Kevin Goodsell
Guest
 
Posts: n/a

re: about virtual inherit


Kevin Goodsell wrote:
[color=blue]
> There are complicated ruled governing when such identifiers are and are not allowed.[/color]

I keep making this same damn typo. That should have been "There are
complicated rules...", not "ruled".

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

  #6  
Old July 19th, 2005, 06:29 PM
Kevin Goodsell
Guest
 
Posts: n/a

re: about virtual inherit


Jonathan Mcdougall wrote:
[color=blue][color=green][color=darkred]
>>>main()[/color]
>>
>>This must be
>>
>>int main()
>>
>>There is no 'implicit int' rule in C++, and there hasn't been for many
>>years.[/color]
>
>
> There never was.[/color]

Not under the ISO standard, but there was before that.

http://www.research.att.com/~bs/sibling_rivalry.pdf

See section 2.6 "From ARM C++ to C++98"

Quote:
Finally, after years of debate, ‘‘implicit int’’ was banned. That is,
every declaration must contain a type. The rule that the absence of a
type implies int is gone. This simplifies parsing, eliminate some
errors, and improves error messages.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: virtual inheritance Juha Nieminen answers 0 June 27th, 2008 05:35 PM
Virtual inheritance and multiple files Klaas Vantournhout answers 3 November 20th, 2006 08:35 AM
Non-virtual methods - why? Adrian Herscu answers 32 November 15th, 2005 12:10 PM
Virtual functions and virtual base classes - I'm confused Michael Winter answers 9 July 19th, 2005 07:15 PM