Exceeding C ++ enforcement method in Concrete class

Is there a way in C ++ to write a specific class that, when another class is derived from it, has a method that needs to be overridden. An abstract class allows forcing a derived class to create specific versions of any abstract methods, but what I want is a base class that applies this, but can also be used on its own. I know that an abstract method can also define default functionality, but it still remains an abstract class that cannot be created.

I also looked at the template template template, but that doesn't seem to be exactly what I'm looking for.

+7
source share
5 answers

I assume that you are looking for a compromise fulfillment of this condition (thanks @Chad for pointing it out)

In C ++, there is no direct language mechanism that I know of. I mean that the reserved keyword will not be placed before the declaration of the method that will achieve the desired goal.

I think what you say indicates a design problem in your software. Suppose you want the foo () method to be overridden by all inheritance classes in the following snippet

class BaseButConcrete { ... //Common stuff ... // virtual void foo() { /*implementation that you do not want to be inherited, but will be...*/ } } class DerivedOtherConcrete : public BaseButConcrete { void foo() { /*different implementation, but no obligation from compiler point of view*/ } } 

I see no good design reason why all ordinary things cannot be moved in an abstract base class. From what you described, you do not want to inherit the foo implementation in Derived, so do not inherit this part! So a very classic design should do the trick:

 class AbstractBase { ... //Common stuff has moved here ... // virtual void foo() =0; } class NotAnymoreBaseButStillConcrete : public AbstractBase { void foo() { /*implementation that should not be inherited, and that will not by design*/ } } class DerivedOtherConcrete : public AbstractBase { void foo() { /*different implementation, now mandatory compiler-wise for the class to be concrete*/ } } 

This way, the common stuff is still distributed among all your derived classes, and you keep what you don't want to inherit (i.e. the implementation of foo ), separated in classes on more than one inheritance path.

+4
source

One option is to put the entire class implementation in an abstract superclass and inherit from it.

  • Move the entire implementation of your specific class T to the abstract class S.
  • In S, make the required overridden method a pure virtual function and define it.
  • T subclasses S.
  • In T, override the required overridden function and call the implementation of S.
  • Instead of subclass T, subclass S.
  • Subclassification T.
+1
source

As already mentioned, you can “fix” this for one derivation level by dividing your current concrete class into an abstract class and a concrete class.

An unusual generalization of this approach is the presence of a class template, which should be used to create a specific class from each abstract class. For example, it can provide a clone method. Or, in Microsoft ATL, it implements an implementation of the IUnknown interface.

However, the abstract class hierarchy with auto-generated concrete leaves as a whole is too much work and complication. A practical approach is to simply document instead what needs to be redefined, for example clone . You cannot force other code to be correct at all, you can only help it in that direction.

By the way, if you could be more specific regarding your specific problem, then there may be much better answers than this general wish.

However, by summarizing your problem, you have summarized the answers.

Cheers and hth.,

+1
source

No, It is Immpossible. If you want to use (at compile time) a derived class to define a virtual function, then its base class must be abstract, or, in other words, the base class must have a pure virtual function. An abstract class (with a pure virtual function (s)) cannot be concrete, i.e. You cannot create it.

0
source

I use the assert macro in the base class to ensure that the function fails during the development testing.

 class MyBaseClass { protected: virtual void MyOverrideFunction(void) { ASSERT(false); // this function must be overridden } } 
0
source

All Articles