473,326 Members | 2,102 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,326 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 2609
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). --...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.