Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ override pure virtual method with pure virtual method

Does it ever make sense to override a pure virtual method with another pure virtual method? Are there any functional differences or perhaps code style reasons to prefer one of the following options over the other?

class Interface {
 public:
  virtual int method() = 0;
};

class Abstract : public Interface {
 public:
  int method() override = 0;
};

class Implementation : public Abstract {
 public:
  int method() override { return 42; }
};

Versus:

class Interface {
 public:
  virtual int method() = 0;
};

class Abstract : public Interface {};

class Implementation : public Abstract {
 public:
  int method() override { return 42; }
};
like image 423
AffluentOwl Avatar asked Apr 17 '15 19:04

AffluentOwl


People also ask

Can you override a pure virtual function?

A pure virtual function is a function that must be overridden in a derived class and need not be defined. A virtual function is declared to be “pure” using the curious =0 syntax.

Should I use Virtual with override?

So the general advice is, Use virtual for the base class function declaration. This is technically necessary. Use override (only) for a derived class' override.

What happens if the pure virtual function is not overridden in the derived class in C++?

3) If we do not override the pure virtual function in derived class, then derived class also becomes abstract class.

What is the purpose of override C++?

Function overriding in C++ is a feature that allows us to use a function in the child class that is already present in its parent class. The child class inherits all the data members, and the member functions present in the parent class.


2 Answers

Both codes produce the same effect: class Abstract is abstract and you can't instantiate it.

There is however a semantic difference between the two forms:

  • The first form reminds clearly that the class Abstract is abstract (just in case it's name would not be self-speaking enough ;-) ). Not only does it reminds it: it also ensures it by making sure that method is pure virtual.
  • The second form means that the class Abstract inherits everything exactly from Interface. It's abstract if and only if its base class is.

This has consequences on future evolutions of your code. For instance, if one day you change your mind and want interface to have a default implementation for method() :

  • In the first form Abstract remains abstract and will not inherit the default implementation of the method.
  • The second form ensures that Abstract would continue to inherit and behave exactly as Interface.

Personally I find that the second form is more intuitive and ensures better separation of concerns. But I can imagine that there could be some situations were the first form could really make sense.

like image 199
Christophe Avatar answered Oct 22 '22 09:10

Christophe


The pure specification on a method forces an override, but it does not prevent you from providing an implementation of the method. The following is a rare, but sometimes useful technique.

class Interface
{
   virtual void method() = 0;
};

class Abstract : public Interface
{
   virtual void method() = 0;
}
inline void Abstract::method() 
{ 
    do something interesting here;
}

class Concrete : public Abstract
{
   virtual void method();
}

inline void Concrete::method()
{
    // let Abstract::method() do it's thing first
    Abstract::method();
    now do something else interesting here;
 }

This is sometimes useful if there are several classes derived from Abstract which need some common functionality, but also need to add class specific behavior. [and should be forced to provide that behavior.]

like image 23
Dale Wilson Avatar answered Oct 22 '22 10:10

Dale Wilson