I've discovered some code that i'm dealing with, and wondered exactly what the best design implementation is.

If your base class defines a technique as virtual, but implements a empty body too, thus not needing the derived classes from applying an appearance, should it 't be made pure rather?

virtual void AMethod1()                  // 1

virtual void AMethod2()    // 2

virtual void AMethod3() =                // 3

  1. current code
  2. idea1: alerts user this derived object hasn't implemented this process body
  3. idea2: pressure derived classes to implement an appearance, empty or otherwise

Exactly what do you, the reliable, amazing SO people think?


edit1: After posting (and reading through solutions), I understand that assert isn't good!

virtual void AMethod3() =                // 4

It is dependent a little how "pure" your coding style is. Many people believe that you ought to always define an interface completely with virtual functions only and derive all concrete classes from that.

Other medication is more practical and belive when there's a great default implementation, you can include that towards the base class (option 1).

The 2nd option appears to become minimal helpful, because it delays recognition until runtime. Most developers prefer to should you prefer a compilation error from option 3.

As always, C++ supports several paradigms and you will pick the one you want.

You need to use option 3 when the derived class must implement this process. Use option 1 if applying within the derived class is optional. Avoid option 2 altogether.

If your base class defines a technique as virtual, but implements a empty body too, thus not needing the derived classes from applying an appearance, should it 't be made pure rather?

That is dependent on whether you need to pressure derived classes to override the technique. Should you choose, then make use of a pure virtual it's language support for just that requirement. If there's or may in a later point be considered a default implementation of amethod, then make use of a pure virtual method by having an implementation:

class Foo 

void Foo::amethod() 

The function has become still pure virtual therefore the Foo class can't be instantiated, but any derived class will inherit the implementation and it is techniques can refer to it as as Foo::amethod.

Making method pure virtual is much more intuitive than making default implementation by having an assert. Current code is much better if not doing anything is default implementation generally. Obviously it will stay virtual if you wish to use polymorphism.

//I've come across a number of good examples such as this where you have to instantiate the category so

//you utilize virtual having a empty body

virtual void AMethod1()                  // 1

//make use of this when you wish to pressure derived class to override

//this function and you do not need a default

virtual void AMethod3() =                // 3

Therefore it is really rely on what for you to do.

  • virtual void AMethod1() = : A pure virtual is the greatest whenever your base class doesn't have implementation to supply AND if this behavior SHOULD be implemented. (This really is option 3 inside your question)

  • virtual void AMethod1() : An online by having an empty implementation is the greatest whenever your base class doesn't have implementation to supply AND if this behavior MAY be implemented. (This really is option one in your question)

  • virtual void AMethod1() : An online by having an assert(false) should be prevented for me. I do not use whatever valid use for this. The explanation behind that being that use cases are handled by the 2 options above: either the behavior may or should is implemented, so there's no use to define a callable method have a tendency to fails. The compiler are designed for that for you personally by stopping this call, which means this option introduces a danger by putting off this checking towards the run time. (This really is option 2 inside your question)