474,028 Members | 2,555 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1739
"DarkSpy" <co****@21cn.co m> wrote in message
news:aa******** *************** **@posting.goog le.com
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
2240
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
1924
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
1900
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 */ } }; class D_of_ABase : public ABase
3
4566
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 = base class Triangle, Square = classes derived from Shape Prism = class derived from Shape TriangularPrism, SquarePrism = classes derived from Triangle and Prism, or Square and Prism respectively
5
2483
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
9393
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 : public Base {
23
4646
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
2180
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: http://mainapp/subapp (which is a 1.1 site)
3
2569
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 used, then "the responsibility for initializing a virtual base is borne by the most derived class in the hierarchy". What does it mean? Initializing base class is usually done automatically by the compiler, but a derived class can invoke the base...
0
10507
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10313
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
12085
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11580
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11932
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
11101
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8659
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7821
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
5366
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.