473,671 Members | 2,216 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Invoking A Base class constructor

Consider the code fragment;
class A
{
public:
A(){}
A(int prm):mprm(prm){ }
int mprm;
};
class B:public A
{
public:
B(){}
B(int prm):A(prm){}

};
class C:public B//,public virtual A
public:
c(){}
C(int prm):A(prm){}
};
int main()
{

C c(3);
}

well my compiler says that 'A' is not a base class of 'C' so i assume
you can only initialize
your immediate base class(why?),but if i remove the comment and
virtually inherit from 'A' the code runs ok, but i feel like i am
creating two A objects (default A constructor called) which is not the
intended behavior, is that true?Does virtual inheritance guarantee only
one 'A' is created?

Jan 7 '07 #1
3 2238
hurcan solter wrote:
Consider the code fragment;
class A
{
public:
A(){}
A(int prm):mprm(prm){ }
int mprm;
};
class B:public A
{
public:
B(){}
B(int prm):A(prm){}

};
class C:public B//,public virtual A
public:
c(){}
C(int prm):A(prm){}
};
int main()
{

C c(3);
}

well my compiler says that 'A' is not a base class of 'C' so i assume
you can only initialize your immediate base class(why?),
Since an object is initialized only once, C cannot initialize A, because B
already does that. You don't need that anyway.
but if i remove the comment and virtually inherit from 'A' the code runs
ok,
That's because with virtual inheritance, B can't initialize A. Think of four
classes building up the diamond structure:

class A {};
class B : virtual public A {};
class C : virtual public A {};
class D: public B, public C {};

Now since B and C both share one A, they can't both initialize it.
Therefore, D gets to initialize the A part.

Jan 8 '07 #2
"hurcan solter" <hs*****@gmail. comwrote in message
news:11******** **************@ 42g2000cwt.goog legroups.com...
Consider the code fragment;
class A
{
public:
A(){}
A(int prm):mprm(prm){ }
int mprm;
};
class B:public A
{
public:
B(){}
B(int prm):A(prm){}

};
class C:public B//,public virtual A
public:
c(){}
C(int prm):A(prm){}
};
int main()
{

C c(3);
}

well my compiler says that 'A' is not a base class of 'C' so i assume
you can only initialize
your immediate base class(why?),but if i remove the comment and
virtually inherit from 'A' the code runs ok, but i feel like i am
creating two A objects (default A constructor called) which is not the
intended behavior, is that true?Does virtual inheritance guarantee only
one 'A' is created?
Class C is inherited from class B, so it contains a class B.
Class B is inhertied from class A, so it contains a class A.

In the C constructor, you need to initialize class B, which will initialize
class A.

C(int prm): B(prm) {}

So your final class C will have one instance of B and one instance of A (if
they're even called instances in this case, I think they are).
Jan 8 '07 #3

hurcan solter wrote:
A(){}
In the case "mprm" is undefined. Write " A():mprm(0){} "
well my compiler says that 'A' is not a base class of 'C' so i assume
you can only initialize your immediate base class(why?),
Because immediate base class already initialize all other base classes.
Each base class does not know any about own derived, so do init. Do you
want to initialize twice? Or do you want to know all base classes of
the creating class?
Does virtual inheritance guarantee only one 'A' is created?
Virtual inheritance guarantees, that class A will be initialized by
constructor of real class of creating object, not by constructor of any
its base classes _where_ class A declared as virtual too.

It is similar to virtual members, which are guarantee, that compiler
will call member of real class of object, not member of class of
pointer or reference.

Virtual inheritance turn constructor of class A as virtual member, so
in each class with the virual base, you must write concrete ctor of
virual base, but compiler will call concrete ctor, declared only in
ctor of real class of creating object.

It can be more questions here, but virtual inheritance is enough rare
used, and in most cases (especially for novice) the kind of inheritance
can be replaced by "adapter" design pattern. Note, that ordinary
inheritance in most cases can be replaced by composition too and the
last is better.

It is seems to me, that virtual inheritance is "bad" only as
"inheritanc e", not as "virtual" or "multiple".

Jan 12 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
4247
by: Lefevre | last post by:
Hello I recently had troubles with a class inheritance hierarchy. I solved it, but it didn't satisfied me. I found the solution using this forum :) Actualy i found the following message (with no responces associated) :
7
28983
by: Dominique | last post by:
Suppose I have: class A { A(int x, int y); }; and class B: public A {
5
7337
by: mrstephengross | last post by:
Ok, I've got code that looks something like this: ================================================== template<typename T1, typename T2> class Base { public: explicit Base(const T1 & t1) { /* ... */ } };
3
3488
by: J.J. Feminella | last post by:
(Please disregard the previous message; I accidentally sent it before it was completed.) I have source code similar to the following. public class Vehicle { protected string dataV; // ... more protected fields }
3
1589
by: hazz | last post by:
The following classes follow from the base class ' A ' down to the derived class ' D ' at the bottom of the inheritance chain. I am calling the class at the bottom, "public class D" from a client app with; D m_D = new D(tkn); public class A : MarshalByRefObject public A () <--------------------- public class B : A public B () <----------------------
6
2489
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
12
3430
by: Hemanth | last post by:
Hi, I have a base class with a static constructor and some abstract methods. Derived classes implement these methods. From articles on the web, it appears that there is no guarentee that this static constructor of the base class would be invoked even if a an object of the derived class is created. Is this correct ? Is there any way to ensure the base class's static constructor is invoked before the derived class instance is constructed ?...
26
5357
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is because I am not always able to control/create all the different constructors the base class has. My problem can be described in code as follows ... /* This is the base class with a whole heap of constructors/functionality*/ public class Animal
0
8473
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
8390
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,...
1
8597
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
8667
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...
0
4222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4402
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2808
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
2
2048
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.