Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overload of pure virtual function

I usually use pure virtual functions for those methods that are required by my code to work well. Therefore, I create interfaces and then other users implement their derived classes. The derived classes have only these virtual functions as public while some additional methods should be implemented as private since my code does not call them. I don't know if this can be considered as a good practice of OOP (are there any design pattern?). Anyway, my question is: Can a user overload a pure virtual function?

i.e.

class Base
{
public:
 Base();
 virtual ~Base();
 virtual void foo(int,double)=0;
};

class Derived:
public Base
{
 private:
  // methods
 public:
 Derived();
 virtual ~Derived();
 virtual void foo(int, double, double); //this doesn't work
 };

A solution could be:

 virtual void foo(int,double,double=0)=0;

in the base class but it is very limited. What do you think about?

like image 612
Ale Avatar asked Apr 05 '13 06:04

Ale


People also ask

Can a pure virtual function be overloaded?

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.

Can we overload pure virtual function C++?

C++ static code analysis: Pure "virtual" functions should not override non-pure "virtual" functions.

Can virtual function be overloaded in Java?

The Virtual function cannot be private, as the private functions cannot be overridden. A virtual function or method also cannot be final, as the final methods also cannot be overridden. Static functions are also cannot be overridden; so, a virtual function should not be static.

Is an overloaded function is called a virtual function?

A Virtual function is different from an Overloaded function. A Virtual function must have similar definitions everywhere i.e. the function prototype defined in the base class must be identical to that of the function defined in the desired class.


1 Answers

These 2 functions are different. The latter is not overriding the first

virtual void foo(int,double)=0;
virtual void foo(int, double, double);

The second one is new virtual function specific to derived.

If you put a override at the end the compile will complain that you are not overriding anything. This is c++11 check though.

virtual void foo(int, double, double) override;

The user can override a pure virtual function to confirm use override at the end of function to verify. In your case the second function can only be accessed using Derived pointer or type. (although it cannot be instantiated unless the pure virtual function is properly overridden and implemented, untill then it is an abstract class). Hence if it is not to be intended to be overidden further by classes that derives from Derived then making it virtual is a overhead as anyway it is not overidding the base method.

like image 141
Abhijit-K Avatar answered Sep 19 '22 14:09

Abhijit-K