473,765 Members | 2,121 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multiple inheritance

Hi everybody I have the following class declarations:

class Interface
{
public:
virtual char* getName() const = 0;
};

class BaseClass : public Interface
{
public:
BaseClass() { }
virtual char* getName() const { return name; }
private:
char* name;
};

class Comparable : public Interface
{
public:
virtual int compare(Interfa ce* value) const = 0;
};

class Person : public BaseClass, public Comparable
{
public:
Person() { }
virtual int compare(Interfa ce* value) const { return 0; }
};
Everything looks ok (for me), but when I try to:
Person* p = new Person();

the compiler (g++) says to me that I cannot allocate an object of type
Person because it has virtual abstract methods.

I think that the compiler cannot figure out that the implementation of
getName() [defined in BaseClass and in Comparable] has already provided
by BaseClass.

I have some misconception problem or it is a problem specific to g++?

Thanks in advance,

Ernesto

Oct 6 '06 #1
3 2654
ernesto wrote:
Hi everybody I have the following class declarations:

class Interface
{
public:
virtual char* getName() const = 0;
};

class BaseClass : public Interface
{
public:
BaseClass() { }
virtual char* getName() const { return name; }
private:
char* name;
};

class Comparable : public Interface
{
public:
virtual int compare(Interfa ce* value) const = 0;
};

class Person : public BaseClass, public Comparable
{
public:
Person() { }
virtual int compare(Interfa ce* value) const { return 0; }
};
Everything looks ok (for me), but when I try to:
Person* p = new Person();

the compiler (g++) says to me that I cannot allocate an object of type
Person because it has virtual abstract methods.
You need to make Interface a virtual base of BaseClass and Comparable.
See http://www.parashift.com/c++-faq-lit...heritance.html, and
especially
http://www.parashift.com/c++-faq-lit....html#faq-25.9

Nate
Oct 6 '06 #2

ernesto wrote:
class Interface
{
public:
virtual char* getName() const = 0;
};

class BaseClass : public Interface
{
public:
BaseClass() { }
virtual char* getName() const { return name; }
private:
char* name;
};

bad interface.
Believe me you don't want to return a pointer to a writeable char
(string).

virtual const char *getName(void) const = 0;

would be better.

virtual const std::string &getName(voi d) const = 0;

would be best
And don't store the name as a char* -- use std::string:
class BaseClass : public virtual Interface
{
public:
BaseClass() { }
virtual const std::string &getName() const { return name; }
private:
std::string name;
};

Oct 6 '06 #3
ernesto wrote:
Hi everybody I have the following class declarations:

class Interface
{
public:
virtual char* getName() const = 0;
};

class BaseClass : public Interface
{
public:
BaseClass() { }
virtual char* getName() const { return name; }
private:
char* name;
use std::string instead, name can be a pointer to nothing, to a single
character or to an array of characters and maybe even to a
null-terminated sequence of characters. Why deal with the ambiguity?
Why deal with the uncertainty of a pointer?
};

class Comparable : public Interface
{
public:
virtual int compare(Interfa ce* value) const = 0;
};

class Person : public BaseClass, public Comparable
{
public:
Person() { }
virtual int compare(Interfa ce* value) const { return 0; }
};
Everything looks ok (for me), but when I try to:
Person* p = new Person();

the compiler (g++) says to me that I cannot allocate an object of type
Person because it has virtual abstract methods.
Yes, thats right. both Interface::getN ame() and
Comparable::com pare(...) are pure virtual. The code you wrote does not
satisfy that requirement since Comparable is not "using" the same
Interface as Baseclass.
>
I think that the compiler cannot figure out that the implementation of
getName() [defined in BaseClass and in Comparable] has already provided
by BaseClass.
Wrong determination. The way you wrote the code, there are 2 distinct
interfaces. And thats the problem. Read up on the dreaded diamond and
virtual inheritance.
http://www.parashift.com/c++-faq-lit...heritance.html
section 25.8, 25.9 and 25.10

Unlike Java, were creating garbage is perfectly acceptable, C++ holds
you responsible for all allocations and deallocations. So try not using
pointers and new until you understand the implications.
And don't listen to people that suggest that its ok to leave garbage
lying around since the program will end anyways. Thats bullshit of the
highest magnitude.
In compare(), for example, rely on a const reference instead. It will
do away with the bugs.
>
I have some misconception problem or it is a problem specific to g++?
g++ is correct.
I'm not getting into an arguement about what Interface should or should
not have. Lets leave that for another day.
In order to coagulate or join an inheritance tree at a single
interface, one uses virtual inheritance. That will fix the fact that
Comparable does not define getName().
Test the code below by removing the virtual BaseClass d~tor. You'll be
in for a surprise.
As long as the Interface and Comparable have no members, they don't (or
shouldn't?) need a virtual destructor. Can someone please confirm that?

#include <iostream>
#include <ostream>
#include <string>

class Interface
{
public:
~Interface() { std::cout << "~Interface()\n "; }
virtual std::string getName() const = 0;
};

class BaseClass : virtual public Interface
{
std::string name;
public:
BaseClass(std:: string s) : name(s) { }
virtual ~BaseClass() { std::cout << "~BaseClass()\n "; }
std::string getName() const { return name; }
};

class Comparable : virtual public Interface
{
public:
~Comparable() { std::cout << "~Comparable()\ n"; }
virtual bool compare(const Interface& value) const = 0;
};

class Person : public BaseClass, public Comparable
{
public:
Person(std::str ing s) : BaseClass(s) { }
~Person() { std::cout << "~Person()\ n"; }
bool compare(const Interface& value) const
{
return getName() == value.getName() ;
}
};

int main()
{
Person person1("Annie" );
BaseClass* p2 = new Person("Betty") ; // carefull: base pointer

std::cout << "Does person1 have the same name as peson2? ";
std::cout << (person1.compar e(*p2) ? "yes" : "no");
std::cout << std::endl;

delete p2; // correctly invokes the Person d~tor
// if and only if ~BaseClass() is virtual
Person person3("Annie" );

std::cout << "Does person1 have the same name as peson3? ";
std::cout << (person1.compar e(person3) ? "yes" : "no");
std::cout << std::endl;

return 0;
}

/*
Does person1 have the same name as peson2? no
~Person() // delete p2 correctly invokes ~Person()
~Comparable()
~BaseClass()
~Interface() // p2 is actually finally zapped here
Does person1 have the same name as peson3? yes
~Person()
~Comparable()
~BaseClass()
~Interface()
~Person()
~Comparable()
~BaseClass()
~Interface()
*/

It might be useful to note that a d!tor declared as virtual propagates
its virtuality up the ladder. The Person d~tor is therefore virtual as
written above. So it would be perfectly safe to derive from that Person
class and continue to use a BaseClass pointer for allocations /
deallocations.
A better name for BaseClass would probably be Character, Unit, Sapien,
Player or Mammal.

Oct 6 '06 #4

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

Similar topics

2
4338
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
5
2182
by: Morgan Cheng | last post by:
It seems no pattern defined by GoF takes advantage of multiple inheritance. I am wondering if there is a situation where multiple inheritance is a necessary solution. When coding in C++, should multiple inheritance still be avoided? If yes, why multiple inheritance is introducted into C++?
20
10084
by: km | last post by:
Hi all, In the following code why am i not able to access class A's object attribute - 'a' ? I wishto extent class D with all the attributes of its base classes. how do i do that ? thanks in advance for enlightment ... here's the snippet #!/usr/bin/python
22
23383
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?
47
3646
by: Mark | last post by:
why doesn't .NET support multiple inheritance? I think it's so silly! Cheers, Mark
60
4936
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the 'target' programming community herein) to get some community input and verify (or not) the following two statements. Few programmers (3 to7%) UNDERSTAND 'Strategic Functional Migration
15
28374
by: iKiLL | last post by:
hi all, I would like to be able to create an umbrella class for all my main global sections but I would still like to keep them all in separate file something like the below but I keep getting an error saying you are not allowed Multiple base classes. /// <summary>
7
3740
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
47
4033
by: Larry Smith | last post by:
I just read a blurb in MSDN under the C++ "ref" keyword which states that: "Under the CLR object model, only public single inheritance is supported". Does this mean that no .NET class can ever support multiple inheritance. In C++ for instance I noticed that the compiler flags an error if you use the "ref" keyword on a class with multiple base classes. This supports the above quote. However, under the "CodeClass2.Bases" property (part...
2
2585
by: Paul McGuire | last post by:
On May 25, 8:37 am, Michael Hines <michael.hi...@yale.eduwrote: Here's a more general version of your testing code, to detect *any* diamond multiple inheritance (using your sample classes). -- Paul for cls in (A,B,C,D): seen = set()
0
10156
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
10007
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
9951
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
9832
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
8831
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
5275
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
5419
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2805
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.