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

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(Interface* value) const = 0;
};

class Person : public BaseClass, public Comparable
{
public:
Person() { }
virtual int compare(Interface* 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 2619
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(Interface* value) const = 0;
};

class Person : public BaseClass, public Comparable
{
public:
Person() { }
virtual int compare(Interface* 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(void) 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(Interface* value) const = 0;
};

class Person : public BaseClass, public Comparable
{
public:
Person() { }
virtual int compare(Interface* 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::getName() and
Comparable::compare(...) 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::string 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.compare(*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.compare(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
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
5
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...
20
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...
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...
47
by: Mark | last post by:
why doesn't .NET support multiple inheritance? I think it's so silly! Cheers, Mark
60
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...
15
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...
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...
47
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...
2
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). --...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
1
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...
0
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.