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

前端 未结 5 1095
南方客
南方客 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-14 16:10

    The response is assuming that the derived class wants to be a CONCRETE class or a non abstract class, i.e it is desired to be able to instantiate objects of type 'Derived'.. Also I assume public member functions in my response.

    No. The derived class has to implement all the pure virtual functions which it inherits from all the base classes. In this case 'Base::myfunction' though inherited by 'Derived' is not treated as an implementation of 'Derived::myfunction'

    'Derived::myfunction' still has to provide an implementation of 'Interface::myfunction'.

    One possibility may be that 'Derived::myfunction' can internally delegate to 'Base::myfunction'

    However if it not desired for Derived to be a concrete class (which I doubt is the intent of OP), then the above arrangement of classes is fine (from language perspective)

提交回复
热议问题