Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing interfaces in C++ with inherited concrete classes

I am trying to define interface types in C++ using abstract classes and implement them with concrete classes. The problem I am running into is that I cannot both inherit and interface from another interface and inherit the implementation from a base concrete class.

My goal is to be able to define a hierarchy of interfaces which may inherit from less complex base interfaces. I also want to be able to extend the implementation of interfaces by inheriting from concrete classes (like inheriting from TObjectA in the example below).

This is what I have. The error I am getting is "object of abstract class type "TObjectB" is not allowed". I believe I know why, which is because I didn't implement MethodA() in TObjectB. But I really want is to have the implementation provided by a base class (TObjectA) and still have interface hierarchies (IInterfaceB inherits from IInterfaceA). I also don't want to repeat all the inherited interface methods in my derived concreate classes. How can I do this?

class IInterfaceA 
{ 
public:
    virtual void MethodA() = 0; 
}; 

class IInterfaceB : IInterfaceA
{ 
public:
    virtual void MethodB() = 0; 
}; 

class TObjectA : public IInterfaceA 
{ 
public:
    void MethodA() { cout << "Method A"; }  
}; 

class TObjectB : public TObjectA, public IInterfaceB 
{ 
public:
    void MethodB() { cout << "Method B"; } 
}; 

void TestInterfaces()
{
    IInterfaceB* b = new TObjectB(); // error: object of abstract class type "TObjectB" is not allowed
    b->MethodB();
    delete b;
}
like image 895
sysrpl Avatar asked Feb 22 '12 23:02

sysrpl


1 Answers

In your class hierarchy, TObjectB actually has two IInterfaceA base class subobjects: one inherited through IInterfaceB and one inherited through TObjectA. The MethodA() from each of them needs to be implemented.

You need to inherit from the interface classes using public virtual inheritance, which ensures there is only a single base class subobject of each interface class type:

class IInterfaceA 
{ 
public:
    virtual void MethodA() = 0; 
}; 

class IInterfaceB : public virtual IInterfaceA
{ 
public:
    virtual void MethodB() = 0; 
}; 

class TObjectA : public virtual IInterfaceA 
{ 
public:
    void MethodA() { cout << "Method A"; }  
}; 

class TObjectB : public TObjectA, public virtual IInterfaceB 
{ 
public:
    void MethodB() { cout << "Method B"; } 
}; 

void TestInterfaces()
{
    TObjectB b_object;
    IInterfaceB& b = b_object;
    b.MethodB();
}

Whether or not such complex class hierarchies are desirable is another matter altogether.

like image 135
James McNellis Avatar answered Oct 20 '22 00:10

James McNellis