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

Interfaces, multiple inheritence, and ambiguous access

Hi,

I am trying to create a class heirarchy similar to the following:

// base interface
class ICar {
public: virtual void start() = 0;
};

// add members to that interface, but retain base capabilities
class IChevy : public ICar {
public: virtual void breakdown() = 0;
};

and here are the implementations:

class Car : public ICar {
public: virtual void start { // start the car }
};

class Chevy :
public IChevy, // we want to create a Chevy
public Car { // but don't want to have to re-implement car from
scratch
public: virtual void breakdown() { start(); }
};

using these class definitions, consider the following main function:

void main(int argc, char * argv)
{
ICar * car = new Car();
car->start();

ICar * car2 = new Chevy();
car2->start();
}

The compiler complains that it cannot instantiate Chevy because it is
abstract. The compiler apparently cannot pick up the implementation of
ICar::start() from the base class of Car. Fair enough. I was hoping to
avoid duplication of code, but I grit my teeth and add the following
to Chevy:

virtual void start() { Car::start(); }

I now get a complaint about the line ICar * car2 = new Chevy(). The
compiler says "ambiguous conversions from 'Chevy *' to 'ICar *'". If,
while playing around and trying to fix the problem, I delete the last
two lines of main() and replace them with the following:

void main(int argc, char * argv)
{
ICar * car = new Car();
car->start();

IChevy * chevy = new Chevy();
chevy->start();
chevy->breakdown();
}

The compiler is happy, but the linker is not. It says "inheritence
error LNK2001: unresolved external symbol "public: virtual void
__thiscall IChevy::breakdown(void)" (?breakdown@IChevy@@UAEXXZ)"".

I do not understand why the linker is failing. I am using MSVC++ 7.0
(.net).

The above is, obviously, a reduction of a broader problem I'm having
with larger classes, but contains the same fundamental design. The
bottom line is that I would like to be able to define interfaces that
inherit from each other. Each interface needs to be implemented
because concrete instances of those implementations are used. However,
I don't want to have to duplicate implementation code in sub classes
if possible. But I am hitting compiler and/or linker errors at every
turn, and hacking my original structure to make things work.

What am I failing to understand here? Is there a clean way to have a
heirarchy of interfaces and implementations such as that listed above?

Thanks in advance for the help.

John
Jul 19 '05 #1
1 4117
John wrote:
Hi,

I am trying to create a class heirarchy similar to the following:

// base interface
class ICar {
public: virtual void start() = 0;
};

// add members to that interface, but retain base capabilities
class IChevy : public ICar {
try:

class IChevy : public virtual ICar {
public: virtual void breakdown() = 0;
};

and here are the implementations:

class Car : public ICar {
public: virtual void start { // start the car }
};

class Chevy :
public IChevy, // we want to create a Chevy
public Car { // but don't want to have to re-implement car from
scratch


and again:

public virtual Car {
look up virtual inheritance.

Jul 19 '05 #2

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

Similar topics

21
by: Franco Gustavo | last post by:
Hi, Please help me to understand this, because I don't see what I'm missing. I was reading a lot of examples on Internet that explain that C# doesn't implement multiple inheritance it...
1
by: Fuzzy | last post by:
The thread on 'why NOT to use interface?' has provoked a question - but I'll start a new thread.... Is there any performance difference that anyone is aware of between using interfaces for...
2
by: raffelm | last post by:
Bear with my while I try to explain... I've created these two interfaces: public interface ILoggingEvents { event LoggingWindowClosing OnClosing;} public interface ILogging { ... } I then...
3
by: Flip | last post by:
I'm looking at the O'Reilly Programming C# book and I have a question about extending and combining interfaces syntax. It just looks a bit odd to me, the two syntaxes look identical, but how does...
8
by: John | last post by:
What is the purpose / benefit of using an interface statement? It doesn't seem like anything more than a different way to make a class... (except you can't define any procedures in an interface...
5
by: Neelesh Bodas | last post by:
This might be slightly off-topic. Many books on C++ consider multiple inheritence as an "advanced" concept. Bruce Eckel says in TICPP, volume 2 that "there was (and still is) a lot of...
22
by: RSH | last post by:
Hi, I have been reading on interfaces working on samples I've run across on the web. For the life of me I cannot seem to grasp them. It appears to me that interfaces are simply blueprints to...
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...
10
by: hyperboreean | last post by:
Hi, Probably it has been asked before, but I'll still ask. Why doesn't python provide interfaces trough its standard library? Or it was ever proposed to be included in the language? Zope's...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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...
0
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,...
0
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...
0
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
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...

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.