Connecting Tech Pros Worldwide Help | Site Map

about virtual inherit

DarkSpy
Guest
 
Posts: n/a
#1: Jul 19 '05
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
John Carson
Guest
 
Posts: n/a
#2: Jul 19 '05

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)


Kevin Goodsell
Guest
 
Posts: n/a
#3: Jul 19 '05

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.

Jonathan Mcdougall
Guest
 
Posts: n/a
#4: Jul 19 '05

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

Kevin Goodsell
Guest
 
Posts: n/a
#5: Jul 19 '05

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.

Kevin Goodsell
Guest
 
Posts: n/a
#6: Jul 19 '05

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