Overload of pure virtual function
Asked Answered
K

6

7

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?

Kreager answered 5/4, 2013 at 6:52 Comment(3)
Function overloads represent a different API. An abstract base class represents a consistent API. So no, this doesn't make sense.Unglue
Isn't your example missing class Derived : Base?Italianism
@nonsensical : right, I have just edited. thanksKreager
D
10

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.

Dituri answered 5/4, 2013 at 7:2 Comment(3)
I was just writing about override when this answer popped up. So here goes at least the link: msdn.microsoft.com/en-us/library/vstudio/jj678987.aspxBaby
@Kupto: why in the example, firstly funcC(double =0.0) override works while in the second code it doesn't work?Kreager
@Kreager I have just tried it and it seems that the override keyword in the first example is redundant, if taking the comments into consideration.Baby
H
4

You can't.

Suppose you have a Base pointer, pointing to a Derived object. Having the Base pointer, you have "access" only to the Base's interface (unless you cast to Derived pointer, but this is not what you need).

Howl answered 5/4, 2013 at 6:55 Comment(3)
yes, this is a problem. So the only solution is create an abstract class but not an interface. Isn't it?Kreager
@Kreager - I don't get your point. What's the difference between interface and abstract class in c++?Howl
link, a derived class could have more functions than the abstract class. Instead an interface limits the number of methods (in my opinion).Kreager
N
3

An overloaded function is merely a function with the same name as another, but accepting a different set of parameters, i.e. it is a different function. It has nothing to do with whether one or more overloaded functions is virtual or not.

In the example you presented, I believe a user could overload the pure-virtual function, but only after overriding it. And you couldn't access the function from the base class directly - you'd need to cast the base class pointer to the derived class pointer.

Nimesh answered 5/4, 2013 at 6:58 Comment(0)
I
0

As pointed out by others, it just won't work.

More discretely, here is what happens

 Derived d;
 d.foo(5, 10.0); // Works as expected

 Base &b = d; // b is polymorphic
 b.foo(3,10,4); // foo(int, double, double) is not in class Base, hence compile-time resolution fails!
Incapacious answered 5/4, 2013 at 7:4 Comment(0)
O
0

It is not overriding as the function signatures are different. According to polymorphism rule to override a function the function signatures and types should be same.

In this case these functions are different. virtual void foo(int,double)=0; virtual void foo(int, double, double);

Orontes answered 13/2, 2014 at 11:36 Comment(0)
O
0

Isn't overloading foo in the base class the easiest solution?

class Base
{
public:
 Base();
 virtual ~Base();
 virtual void foo(int,double)=0;
 virtual void foo(int,double,double)=0;
};
Occidentalize answered 10/5, 2015 at 11:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.