Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Under what circumstances is it advantageous to give an implementation of a pure virtual function?

In C++, it is legal to give an implementation of a pure virtual function:

class C { public:   virtual int f() = 0; };  int C::f()  {   return 0; } 

Why would you ever want to do this?

Related question: The C++ faq lite contains an example:

class Funct { public:   virtual int doit(int x) = 0;   virtual ~Funct() = 0; };  inline Funct::~Funct() { }  // defined even though it's pure virtual; it's faster this way; trust me 

I don't understand why the destructor is declared pure virtual and then implemented; and I don't understand the comment why this should be faster.

like image 966
Tobias Avatar asked Jun 10 '09 19:06

Tobias


People also ask

What is the advantage of declaring a virtual function as pure?

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.

Why do we make a virtual function pure What are the implementation of making a function a pure virtual functions?

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 {

Can a pure virtual function have an implementation?

A pure virtual function must be implemented in a derived type that will be directly instantiated, however the base type can still define an implementation.

What could be the advantages to use a virtual method?

The main advantage of virtual functions are that they directly support object oriented programming. When you declare a function as virtual you're saying that exactly what code is executed depends on the type of the object you call it against. you can't tell exactly what code path it's going to follow.


1 Answers

Declared destructors must always be implemented as the implementation will call them as part of derived object destruction.

Other pure virtual functions may be implemented if they provide a useful common functionality but always need to be specialized. In the case, typically derived class implementations will make an explicit call to the base implementation:

void Derived::f() {     Base::f();      // Other Derived specific functionality } 

Typically, you make a destructor virtual if you need to make a class abstract (i.e. prevent non-derived instances from being created) but the class has no other functions that are naturally pure virtual. I think the 'trust me it's faster' is refering to the fact that because destructors called as part of derived object clean up don't need to use a vtable lookup mechanism, the inline implementation can be taken advantage of, unlike typical virtual function calls.

like image 104
CB Bailey Avatar answered Oct 12 '22 05:10

CB Bailey