Is it possible to inherit the implementation of an interface

Philinator

is it possible to inherit the implementation of an interface without implementing it for each implementation?

enter image description here

In the image above IBase and Ichild are classes having only pure virtual methods and Base implements the methods of IBase and Child implements the methods of IChild. Is it now somehow possible for Child to inherit the implementations of the methods of IBase via Base, so that Child does not have to implement these methods itself?

I tried to implement it like this:

struct IBase {
    virtual void do_something_Base() = 0;
};

struct IChild : public virtual IBase {
    virtual void do_something_Child() = 0;
};


struct Base : public virtual IBase {
    virtual void do_something_Base() { //implementation IBase method
        //do sth
    }
};

struct Child : public Base , public IChild {
    virtual void do_something_Child() { //implementation IChild method
        //do sth
    }
};

int main() {

    IBase* B = new Child;
    B->do_something_Base();

    delete B;

    return 0;
}

The problem with this code is that the copiler gives a warning:

warning C4250: 'Child': inherits 'Base::Base::do_something_Base' via dominance

Futhermore the program crashes at the line delete B. (If I use IChild* B instead of IBase* B the program does not crash. why is that?)

Thanks in advance

Angew is no longer proud of SO

I've been using a similar setup in a project, and it works for me. It should be enough to do the following:

  1. Give IBase a virtual destructor. A virtual destructor is necessary for being able to delete through pointers to a base class.

  2. To be on the safe side, derive Child from IChild virtually as well. Since the I-classes have no data, there's no point in using them as non-virtual bases.

  3. Disable the warning, as it's basically telling that the compiler is doing exactly what you want it to do (that the implementation is inherited from the correct base class).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Inherit Interface Implementation

From Dev

Is it possible to inherit the default IShellFolder implementation?

From Dev

Can a C++ interface inherit from a class with full implementation?

From Dev

Is it possible for several classes to share an interface implementation?

From Dev

Can a class inherit implementation of a method contruct imposed by an interface from other class?

From Dev

F# inherit interface

From Dev

Partially inherit an interface in Java?

From Dev

F# inherit interface

From Dev

Is it possible to inject EJB implementation and not its interface using CDI?

From Java

Is it possible to use Java functional interface implementation (such as the Supplier) as a MethodSource in Junit 5?

From Dev

Is it possible to use default interface implementation in Android < API 24?

From Dev

Is an early bound interface and a late bound implementation possible with C#

From Dev

Inherit module in Javascript possible?

From Dev

Inherit custom TabBar and implementation of resizeEvent

From Dev

Why can not inherit from Interface

From Dev

Inherit from two hierarchical interface?

From Dev

Interface inherit from other interface in golang

From Dev

Is it possible to not have a CSS rule inherit?

From Dev

Is it possible to not have a CSS rule inherit?

From Dev

Is it possible to define "interface" that will accept class instance which type/implementation is not known ahead?

From Dev

Why interface cannot inherit pure abstract class

From Dev

Implementing Interfaces That "Inherit" (Implement) A Common Interface?

From Dev

Get names of structs that implement an interface or inherit a struct

From Dev

Is there a way to extend a built-in type to inherit an interface?

From Dev

How can my class inherit an interface and QObject ?

From Dev

Deserialising an implementation of an interface

From Dev

force explicit interface implementation

From Dev

How to inject an interface implementation?

From Dev

Interface implementation with optional arguments

Related Related

  1. 1

    Inherit Interface Implementation

  2. 2

    Is it possible to inherit the default IShellFolder implementation?

  3. 3

    Can a C++ interface inherit from a class with full implementation?

  4. 4

    Is it possible for several classes to share an interface implementation?

  5. 5

    Can a class inherit implementation of a method contruct imposed by an interface from other class?

  6. 6

    F# inherit interface

  7. 7

    Partially inherit an interface in Java?

  8. 8

    F# inherit interface

  9. 9

    Is it possible to inject EJB implementation and not its interface using CDI?

  10. 10

    Is it possible to use Java functional interface implementation (such as the Supplier) as a MethodSource in Junit 5?

  11. 11

    Is it possible to use default interface implementation in Android < API 24?

  12. 12

    Is an early bound interface and a late bound implementation possible with C#

  13. 13

    Inherit module in Javascript possible?

  14. 14

    Inherit custom TabBar and implementation of resizeEvent

  15. 15

    Why can not inherit from Interface

  16. 16

    Inherit from two hierarchical interface?

  17. 17

    Interface inherit from other interface in golang

  18. 18

    Is it possible to not have a CSS rule inherit?

  19. 19

    Is it possible to not have a CSS rule inherit?

  20. 20

    Is it possible to define "interface" that will accept class instance which type/implementation is not known ahead?

  21. 21

    Why interface cannot inherit pure abstract class

  22. 22

    Implementing Interfaces That "Inherit" (Implement) A Common Interface?

  23. 23

    Get names of structs that implement an interface or inherit a struct

  24. 24

    Is there a way to extend a built-in type to inherit an interface?

  25. 25

    How can my class inherit an interface and QObject ?

  26. 26

    Deserialising an implementation of an interface

  27. 27

    force explicit interface implementation

  28. 28

    How to inject an interface implementation?

  29. 29

    Interface implementation with optional arguments

HotTag

Archive