473,414 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

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
Jul 19 '05 #1
5 1707
"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
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
>> 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: nenad | last post by:
Wouldn't it be nice if we could do something like this: class Funky{ public: auto virtual void doStuff(){ // dostuff } };
9
by: jlopes | last post by:
There seems to bet no diff between a vitual method and an inheirited method. class A_Base { public: virtual void filter(){ /* some code */ } }; class D_of_A_Base : public A_Base {
9
by: jlopes | last post by:
I'm looking at the differences between these to forms and see no difference in their use. When accessed through a derived class. class ABase { public: virtual void filter(){ /* some code */ }...
3
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape...
5
by: Noah Roberts | last post by:
Is there anything that says that if you virtually inherit from one class you have to virtually inherit from anything you inherit from?
7
by: Thomas Kowalski | last post by:
Hi everyone, is there a way to enforce that every class that inherit from BASE have to implement a given constructor? Example: class Base { virtual Base (int,int) = 0; } class INHERIT :...
23
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
6
by: Eric | last post by:
Hi Everyone, I have an ASP 1.1 website being hosted in a virtual directory that is a child node of an ASP 2.0 website. So I have: http://mainapp (which is a 2.0 site) And: ...
3
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.