473,224 Members | 1,913 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,224 software developers and data experts.

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 5840

"Jimmy Johns" <as**@asdf123asdf.net> wrote in message
news:bh***********@news.eecs.umich.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******@bellsouth.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**@asdf123asdf.net> wrote in message
news:bh***********@news.eecs.umich.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
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...
22
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...
45
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...
10
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,...
14
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...
7
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...
7
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...
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...
2
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...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.