By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,900 Members | 1,348 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,900 IT Pros & Developers. It's quick & easy.

about virtual inherit

P: 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
Jul 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"DarkSpy" <co****@21cn.com> wrote in message
news:aa*************************@posting.google.co m
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

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)
Jul 19 '05 #2

P: n/a
DarkSpy wrote:
code here:

I don't know the answer to your actual question, but I have a few
comments on your code.
struct A
{
int _i, _j;
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>

main()


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.

Jul 19 '05 #3

P: n/a
>> main()

This must be

int main()

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


There never was.
Jonathan

Jul 19 '05 #4

P: n/a
Kevin Goodsell wrote:
There are complicated ruled governing when such identifiers are and are not allowed.


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.

Jul 19 '05 #5

P: n/a
Jonathan Mcdougall wrote:
main()


This must be

int main()

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

There never was.


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"

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.

Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.