Is there a way to specify or assert that the child class must re-implement a specific non-abstract virtual method?

Violet Giraffe

Here's what I mean. I have a class hierarchy:

class A {
   virtual int f() = 0;
};

class B : public A {
   int f() override {return 5;}
   void doSpecificStuff() {}
}

B is a self-sufficient class that can be used on its own. But it also has many descendants:

class C : public B {
   int f() override {return 171;}
}

Is there any way to make sure that I won't forget to re-implement f when subclassing B?

Angew is no longer proud of SO

This solution is inspired by @dyp's comment:

You could split the two responsibilities of B, namely "provides B-style implementation" and "can be instantiated."

class B_for_implementation : public A
{
  void doSpecificStuff();
};

class B_for_objects final : public B_for_implementation
{
  int f() override {return 5;}
};

class C final : public B_for_implementation
{
  int f() override {return 171;}
};

Self-contained objects would be created from B_for_objects, while other classes would derive from B_for_implementation.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Class must be declared as abstract or implement abstract method

From Dev

Class must either be declared abstract or implement abstract method

From Dev

Class must either be declared abstract or implement abstract method?

From Dev

Class must either be declared abstract or implement abstract method error

From Dev

Class must either be declared abstract or implement abstract method: Intellij error?

From Dev

RxAndroid: Class must either be declared abstract or implement abstract method error

From Dev

Class must either be declared abstract or implement abstract method: Intellij error?

From Dev

Java child class not able to implement parent class's abstract method

From Dev

Error: "class must implement abstract method X", but it already does

From Dev

Abstract method in a non abstract class

From Dev

Java Error: Class 'Anonymous' must either be declared abstract or implement abstract method 'actionPerformed(ActionEvent)' in 'ActionListener'

From Dev

Class SubList() must either be called abstract or implement abstract method 'listIterator' in 'List'

From Dev

"Class contains 1 abstract method so must be declared abstract or implement the remaining methods (parent::hasErrors)"

From Dev

Java Error: Class 'Anonymous' must either be declared abstract or implement abstract method 'actionPerformed(ActionEvent)' in 'ActionListener'

From Dev

Android: Class 'MainActivity2' must either be declared abstract or implement abstract method 'onNothingSelected(AdapterView<?>'

From Dev

Elegant way to implement abstract class

From Dev

Android must implement the inherited abstract method

From Dev

the type must implement the inherited abstract method

From Dev

Specify that a Class argument must implement a particular interface

From Dev

calling non abstract method in abstract class java

From Dev

Abstract class non abstract method invoke

From Dev

How to specify that abstract types must implement certain typeclasses in scalaz?

From Dev

How to specify that abstract types must implement certain typeclasses in scalaz?

From Dev

Calling child class non-virtual method or setting child class property

From Dev

Class CI_Session_files_driver contains 1 abstract method and must therefore be declared abstract or implement the remaining methods

From Dev

How to specify the return type of the method of an abstract class

From Dev

How to specify the return type of the method of an abstract class

From Dev

Best way to implement a method specific to a class but does not directrly uses the attributes

From Dev

Assert specific constructor called by child class

Related Related

  1. 1

    Class must be declared as abstract or implement abstract method

  2. 2

    Class must either be declared abstract or implement abstract method

  3. 3

    Class must either be declared abstract or implement abstract method?

  4. 4

    Class must either be declared abstract or implement abstract method error

  5. 5

    Class must either be declared abstract or implement abstract method: Intellij error?

  6. 6

    RxAndroid: Class must either be declared abstract or implement abstract method error

  7. 7

    Class must either be declared abstract or implement abstract method: Intellij error?

  8. 8

    Java child class not able to implement parent class's abstract method

  9. 9

    Error: "class must implement abstract method X", but it already does

  10. 10

    Abstract method in a non abstract class

  11. 11

    Java Error: Class 'Anonymous' must either be declared abstract or implement abstract method 'actionPerformed(ActionEvent)' in 'ActionListener'

  12. 12

    Class SubList() must either be called abstract or implement abstract method 'listIterator' in 'List'

  13. 13

    "Class contains 1 abstract method so must be declared abstract or implement the remaining methods (parent::hasErrors)"

  14. 14

    Java Error: Class 'Anonymous' must either be declared abstract or implement abstract method 'actionPerformed(ActionEvent)' in 'ActionListener'

  15. 15

    Android: Class 'MainActivity2' must either be declared abstract or implement abstract method 'onNothingSelected(AdapterView<?>'

  16. 16

    Elegant way to implement abstract class

  17. 17

    Android must implement the inherited abstract method

  18. 18

    the type must implement the inherited abstract method

  19. 19

    Specify that a Class argument must implement a particular interface

  20. 20

    calling non abstract method in abstract class java

  21. 21

    Abstract class non abstract method invoke

  22. 22

    How to specify that abstract types must implement certain typeclasses in scalaz?

  23. 23

    How to specify that abstract types must implement certain typeclasses in scalaz?

  24. 24

    Calling child class non-virtual method or setting child class property

  25. 25

    Class CI_Session_files_driver contains 1 abstract method and must therefore be declared abstract or implement the remaining methods

  26. 26

    How to specify the return type of the method of an abstract class

  27. 27

    How to specify the return type of the method of an abstract class

  28. 28

    Best way to implement a method specific to a class but does not directrly uses the attributes

  29. 29

    Assert specific constructor called by child class

HotTag

Archive