Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 19th, 2005, 06:28 PM
DarkSpy
Guest
 
Posts: n/a
Default about virtual inherit

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

 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles