Interfaces, multiple inheritence, and ambiguous access

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John

    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 "inheritenc e
    error LNK2001: unresolved external symbol "public: virtual void
    __thiscall IChevy::breakdo wn(void)" (?breakdown@ICh evy@@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
  • Gianni Mariani

    #2
    Re: Interfaces, multiple inheritence, and ambiguous access

    John wrote:[color=blue]
    > 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 {[/color]

    try:

    class IChevy : public virtual ICar {
    [color=blue]
    > 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[/color]

    and again:

    public virtual Car {


    look up virtual inheritance.



    Comment

    Working...