C++: using a base class as the implementation of an interface

前端 未结 5 1076
南方客
南方客 2020-12-14 16:01

In C++ is it possible to use another base class to provide the implementation of an interface (i.e. abstract base class) in a derived class?

class Base
{
          


        
5条回答
  •  时光取名叫无心
    2020-12-14 16:23

    No. (not that way anyway)

    You might be mislead by the way things are done in other languages like Java, C#, ActionScript, etc.

    In C++, multiple inheritance and the way virtual classes are managed makes interfaces (as used in other languages) obsolete. In those other languages, interfaces are used to fix problems issued from the lack of multiple inheritance (good or bad, it's a choice).

    So if what you want to do is just provide a general interface with some virtual methods providing default implementations, just implement in the base class :

    class Interface
    {
        virtual void myfunction() { /*...*/ } //default implementation
        virtual void yourFunction()  = 0 ; // this one HAVE TO be implemented by the user
    };
    class Derived
        : public Interface // don't need another class
    {
        // myfunction is implemented by base
        void yourFunction(); // have to implement yourFunction
    };
    class DerivedB
        : public Interface // don't need another class
    {
        void myFunction(); // myfunction is implemented by base but we implement it for this specific class
        void yourFunction(); // have to implement yourFunction
    };
    

    If however, you want to provide several base classes that have the same interfaces, then think that your interface class is the bases of the other classes

    // in this order
    class Interface
    {
        virtual void myfunction() = 0;
    };
    class BaseA : public Interface
    {   
        // here "virtual" is optional as if the parent is virtual, the child is virtual too
        virtual void myfunction() {/*...*/}; // BaseA specific implementation
    };
    class BaseB : public Interface
    {
        virtual void myfunction() {/*...*/}; // BaseB specific implementation
    };
    

    There is however a not-really-easy-to-read (read: not recommanded) way to provide a default implementation BUT forcing the user to explicitely say if he want to use it or not. It exploit the fact that even pure virtual functions can have default implementations that can be called :

    class Interface
    {
        virtual void myfunction() { /*...*/ } // default implementation
        virtual void yourFunction()  = 0 ; // this one HAVE TO be implemented by the user BUT provide a default implementation!
    };
    
    // in Interface.cpp 
    
    void Interface::yourFunction() // default implementation of the virtual pure function
    { /*...*/ }
    
    // in Derived.h
    
    class DerivedA
        : public Interface // don't need another class
    {
        // myfunction is implemented by base
        void yourFunction(); // have to implement yourFunction -- DerivedA specific
    };
    
    class DerivedB
        : public Interface // don't need another class
    {
        void myFunction(); // myfunction is implemented by base but we implement it for this specific class
        void yourFunction() { Interface::yourFunction(); } // uses default implementation of yourFunction, hidden but existing
    };
    

    But don't do it.

提交回复
热议问题