71

Pure virtual functions (when we set = 0) can also have a function body.

What is the use to provide a function body for pure virtual functions, if they are not going to be called at all?

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
Vijay
  • 2,021
  • 4
  • 24
  • 33
  • Sorry! I am somewhat new to terms on this forums. I have accepted the answers to what i feeled right. thanks for suggesssions. – Vijay Mar 31 '11 at 06:55

3 Answers3

104

Your assumption that pure virtual function cannot be called is absolutely incorrect. When a function is declared pure virtual, it simply means that this function cannot get called dynamically, through a virtual dispatch mechanism. Yet, this very same function can easily be called statically, non-virtually, directly (without virtual dispatch).

In C++ language a non-virtual call to a virtual function is performed when a qualified name of the function is used in the call, i.e. when the function name specified in the call has the <class name>::<function name> form.

For example

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

void S::foo() 
{
  // body for pure virtual function `S::foo`
}

struct D : S 
{
  void foo() 
  {
    S::foo();       
    // Non-virtual call to `S::foo` from derived class

    this->S::foo(); 
    // Alternative syntax to perform the same non-virtual call 
    // to `S::foo` from derived class
  }
};

int main() 
{
  D d;

  d.S::foo(); 
  // Another non-virtual call to `S::foo`
}
AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
30

"Effective C++" Meyers mentions a reason for a pure virtual function to have a body: Derived classes that implement this pure virtual function may call this implementation smwhere in their code. If part of the code of two different derived classes is similar then it makes sense to move it up in the hierarchy, even if the function should be pure virtual.

see here.

Prince John Wesley
  • 62,492
  • 12
  • 87
  • 94
  • Darn... I have the 12th edition of _Effective C++_ and I missed that statement. The one reason, I think, to define it is because it may get called and the default throws which may not be what you want in your software... (of course, re-using code is also a good reason). – Alexis Wilke Feb 17 '21 at 01:01
  • Link doesn't work anymore – Jimmy T. Feb 24 '23 at 10:13
8

For most pure virtual functions, you'd be right. However, for a pure virtual destructor, it's actually important to define a corresponding destructor implementation:

  • The "pure virtual" is to require derived classes to implement their destructor.
  • Your base class destructor implementation is so that the derived class destructors can successfully "chain up" afterwards.
C. K. Young
  • 219,335
  • 46
  • 382
  • 435
  • 13
    (I realise this answer is a year old, but I've just seen a link referring people to this question, so ...) Your first point is wrong, a pure virtual destructor does not force derived classes to implement a destructor. The derived class will always have a destructor, even if it's implicitly defined. One advantage of a pure virtual destructor is to make a class abstract if it has no other functions suitable for being pure virtual. – Jonathan Wakely May 28 '12 at 18:01