Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does it make any sense to define "pure" virtual functions in the base class itself?

The benefit of defining common virtual functions in the base class is that we don't have to redefine them in the derived classes then.

Even if we define pure virtual functions in the base class itself, we'll still have to define them in the derived classes too.

#include <iostream>
using namespace std;

class speciesFamily
{
    public:
        virtual void numberOfLegs () = 0;
};

void speciesFamily :: numberOfLegs ()
{
    cout << "\nFour";
}

class catFamily : public speciesFamily
{
    public:
        void numberOfLegs ()
        {
            speciesFamily :: numberOfLegs ();
        }
};

This may look fancy for sure, but are there any situations when it is beneficial to define a pure virtual function in the base class itself?

like image 467
Aquarius_Girl Avatar asked Nov 09 '11 10:11

Aquarius_Girl


People also ask

Can a pure virtual function be defined in the base class?

A pure virtual function is a member function of base class whose only declaration is provided in base class and should be defined in derived class otherwise derived class also becomes abstract. Classes having virtual functions are not abstract. Base class containing pure virtual function becomes abstract.

Is it necessary to define 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. For example: class Base {

What is the significance of using a pure virtual function in a class?

A pure virtual function makes it so the base class can not be instantiated, and the derived classes are forced to define these functions before they can be instantiated. This helps ensure the derived classes do not forget to redefine functions that the base class was expecting them to.

Is it legal to have all member functions of a class to be pure virtual functions?

Yes, it is possible to have a pure virtual destructor. Pure virtual destructors are legal in standard C++ and one of the most important things to remember is that if a class contains a pure virtual destructor, it must provide a function body for the pure virtual destructor.


1 Answers

Two things:

First off, there's one border-line scenario which is commonly cited: Suppose you want an abstract base class, but you have no virtual functions to put into it. That means you have no functions to make pure-virtual. Now there's one way around: Since you always need a virtual destructor, you can make that one pure. But you also need an implementation, so that's your canditate:

struct EmptyAbstract
{
  virtual ~EmptyAbstract() = 0; // force class to be abstract
};
EmptyAbstract::~EmptyAbstract() { } // but still make d'tor callable

This may help you minimize the implementation size of the abstract class. It's a micro-opimization somehow, but if it fits semantically, then it's good to have this option.

The second point is that you can always call base class functions from derived classes, so you may just want to have a "common" feature set, despite not wanting any abstract instances. Again, in come pure-virtual defined functions:

struct Base
{
  virtual void foo() = 0;
};

struct Derived : Base
{
  virtual void foo()
  {
    Base::foo();  // call common features
    // do other stuff
  }
};

void Base::foo() { /* common features here */ }
like image 86
Kerrek SB Avatar answered Sep 29 '22 00:09

Kerrek SB