473,698 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

multiple inheritance and constructors

Hi, I have some classes as follows:

#include <iostream>

using namespace std;

class A {
public:
virtual A* clone() const = 0;
};
class B {
public:
B(const B& rhs) : p(0)
{
delete p;
p = new int;
*p = *rhs.p;
}
private:
int* p;
};
class C : public A, public B {
public:
C* clone() const
{
#24 return new C(*this);
}
};
int main()
{
#29 A* pa = new C;
A* pa2 = pa->clone();
}

this code gives the following errors under g++/cygwin:
29: no matching function for call to C::C()
24: candidates are C::C(const C&)

okay... this is puzzling. If I take out B's copy constructor, then it
compiles. It also appears that if I define any sort of new constructors
(default, non-defualt ones), then I cannot use the compiler supplied
defaults. The compiler then complains that it cannot find those default
constructors. Is this just an issue with MI or in general? Also, are there
any solutions to this besides defining all the constructors, copy
constructors in the inheritance hierarchy? Thanks in advance.

JJ.
Jul 19 '05 #1
4 5868

"Jimmy Johns" <as**@asdf123as df.net> wrote in message
news:bh******** ***@news.eecs.u mich.edu...
Hi, I have some classes as follows:

#include <iostream>

using namespace std;

class A {
public:
virtual A* clone() const = 0;
};
class B {
public:
B(const B& rhs) : p(0)
{
delete p;
p = new int;
*p = *rhs.p;
}
private:
int* p;
};
class C : public A, public B {
public:
C* clone() const
{
#24 return new C(*this);
}
};
int main()
{
#29 A* pa = new C;
A* pa2 = pa->clone();
}

this code gives the following errors under g++/cygwin:
29: no matching function for call to C::C()
24: candidates are C::C(const C&)

okay... this is puzzling. If I take out B's copy constructor, then it
compiles. It also appears that if I define any sort of new constructors
(default, non-defualt ones), then I cannot use the compiler supplied
defaults.
If you define any ctor, then the compiler will not generate a default
(no-arg) ctor. It will still generate a copy ctor.
The compiler then complains that it cannot find those default
constructors. Is this just an issue with MI or in general?
In general.
Also, are there
any solutions to this besides defining all the constructors, copy
constructors in the inheritance hierarchy? Thanks in advance.

JJ.


There is no issue, you seem to lack confidence in C++ and assume that it is
going to force you do to lots of work.

By defining a ctor, you lose the default ctor. That makes sense since a
class with no ctors must still be constructed somehow, so the compiler
generates a default ctor. As soon as you write a ctor, you are saying 'I
know how to construct this class, don't generate a default ctor for me'.
Which again is sensible, many classes don't want a default ctor.

None of this has anything to do with copy ctors which are still generated by
the compiler unless you write your own.

john
Jul 19 '05 #2

okay... this is puzzling. If I take out B's copy constructor, then it
compiles. It also appears that if I define any sort of new constructors
(default, non-defualt ones), then I cannot use the compiler supplied
defaults. The compiler then complains that it cannot find those default
constructors. Is this just an issue with MI or in general? Also, are there any solutions to this besides defining all the constructors, copy
constructors in the inheritance hierarchy? Thanks in advance.

JJ.


The compiler supplied copy constructor is a binary copy, so the pointers
within the "B" class point to the same data. Once you have defined one of
the copy constructors you pretty much have to declare them all.

// something like this
C(const &c):B(c) { }
// or maybe this...
C(const &c):B::B(c) { }


Jul 19 '05 #3
but can anyone duplicate the error I'm getting? Right now my project in VC7
has pretty much the same layout, but slightly more complicated, and it still
insists that I define the ctor, copy ctor myself. Is this a bug or
something in the standard?

thanks.

JJ
"John Harrison" <jo************ *@hotmail.com> wrote in message
news:bh******** ****@ID-196037.news.uni-berlin.de...

"John Isaacks" <ji******@bells outh.net> wrote in message
news:vY******** *********@fe05. atl2.webusenet. com...

okay... this is puzzling. If I take out B's copy constructor, then it
compiles. It also appears that if I define any sort of new constructors (default, non-defualt ones), then I cannot use the compiler supplied
defaults. The compiler then complains that it cannot find those default constructors. Is this just an issue with MI or in general? Also, are

there
any solutions to this besides defining all the constructors, copy
constructors in the inheritance hierarchy? Thanks in advance.

JJ.


The compiler supplied copy constructor is a binary copy,


No it isn't, its a memberwise copy.
so the pointers
within the "B" class point to the same data.


That's true.
Once you have defined one of
the copy constructors you pretty much have to declare them all.


But that isn't. Once you have defined a copy constructor for one class you
do *not* have to define it for other classes that use that class. This is
the difference between a memberwise copy and a bitwise copy. The designers
of C++ would have been *extremely* stupid to have gone for a bitwise copy,
fortunately they didn't.

I suggest you write a couple of classes for yourself to test this out.

john

Jul 19 '05 #4

"Jimmy Johns" <as**@asdf123as df.net> wrote in message
news:bh******** ***@news.eecs.u mich.edu...
but can anyone duplicate the error I'm getting? Right now my project in VC7 has pretty much the same layout, but slightly more complicated, and it still insists that I define the ctor, copy ctor myself. Is this a bug or
something in the standard?


No, its not a bug. Think about, your class B does not have a (compiler
generated) default ctor, that's because you defined a copy ctor for B.
Therefore your class C which derives from B cannot have a default ctor
either, because the compiler generated default ctor for C would have to call
the default ctor for B (how could the compiler be smart enough to generate
anything else?). But the default ctor for B doesn't exist and isn't
generated, so the default ctor for C cannot be generated.

So to fix the bug, either write a default ctor for B, this will allow the
compiler to generate a default ctor for C, or write a default ctor for C
which can call whatever ctor for B you like.

john
Jul 19 '05 #5

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

Similar topics

4
2281
by: Busin | last post by:
When a child class inherits from a base class, will the child class inherits everything of the base class, including all member variables and functions? Or is such inheritance "selective", like not inheriting all constructors, assignment operators, destructor, etc.? Thanks!
22
23362
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete examples?
45
6342
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using parameters, I get CS1501 (no method with X arguments). Here's a simplified example which mimics the circumstances: namespace InheritError { // Random base class. public class A { protected int i;
10
1217
by: Kevin Buchan | last post by:
I searched the news group and could not find an answer to this question, so I'll go ahead and post it. Let's say I have a class A with a couple different constructors... nothin' special. Now, let's say that I have class B that inherits from A. B automagically gets all of the properties and methods of A, but I cannot leverage any of A's contructors without redefining them on B and delegating. I'm sure that there is a very good reason...
14
3615
by: dl | last post by:
I have two classes, say A and B, both having a data member 'int n'; private in A, public in B. When I derive class C from both public A and public B, B::n should be visible to C while A::n should not be. But if I compile with g++-4.0.3 the following snippet: class A { int i;
7
16556
by: andrewfsears | last post by:
I have a question: I was wondering if it is possible to simulate the multiple constructors, like in Java (yes, I know that the languages are completely different)? Let's say that I have a class called "Point" which would have two values "x" and "y". Now, let's say if it were the Java version, I would want two constructors: one that accept two numbers, the other accepts a string:
7
3734
by: Adam Nielsen | last post by:
Hi everyone, I'm having some trouble getting the correct chain of constructors to be called when creating an object at the bottom of a hierarchy. Have a look at the code below - the inheritance goes like this: Shape | +-- Ellipse | +-- Circle
3
2551
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...
2
1997
by: Tim Van Wassenhove | last post by:
Hello, When i read the CLI spec, 8.10.2 Method inheritance i read the following: "A derived object type inherits all of the instance and virtual methods of its base object type. It does not inherit constructors or static methods...." In the C# spec, 17.2.1 Inheritance i read the following:
0
8685
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
8612
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
9032
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...
0
8880
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
7743
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5869
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();...
0
4373
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
4625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2342
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.