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

4

13

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?

Pinup answered 9/11, 2011 at 10:56 Comment(2)
possible duplicate of pure virtual function with implementationMateriel
And note that the answers over there have something in common. Other than the special case of destructors, the implementation in the base class will only ever be called explicitly by derived classes. It could just as well have had a different name, so the "benefit" is just that it has the same name as the override expected to call it, saving you from warts like _default or _base.Materiel
E
11

are there any situations when it is beneficial to define a pure virtual function in the base class itself?

Yes - if the function in question is the pure virtual destructor, it must also be defined by the base class.

Exertion answered 9/11, 2011 at 11:12 Comment(1)
Thanks, any other situations?Pinup
C
14

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 */ }
Caril answered 9/11, 2011 at 11:27 Comment(10)
I don't understand the sense of this suppose you want an abstract base class, but you have no virtual functions to put into it. Abstract class means its objects can't be created. Fine. And now if we don't have even virtual functions to put into it, then what sense does it make to create that abstract base class? Any example situations in which this scenario will make sense?Pinup
@AnishaKaul: I never said it made sense! C++ is a rich, complex language that allows you to do all sorts of things that are logically possible on paper. Not all of them are practically useful (like protected inheritance, or function-try-blocks). Who knows, maybe you just want a blank type-erasing base class with no features? Some people who love dynamic casts might have a use for an empty common base class... Actually, the fact that those babies are even hard just to write might indicate that this is not a feature that's expected to be used a lot.Caril
Alright, but I was looking for a real life situation in which defining pure virtual functions in base class would have made actual sense. :)Pinup
If you want an abstract base class it is likely simpler to make the constructors protected.Orelee
@AngelO'Sphere: That doesn't make it abstract; it just makes it harder (but not impossible) to instantiate. It also doesn't make it polymorphic, which is (more or less) the only reason you'd want an abstract class that doesn't declare virtual functions.Manx
@Mike Seymour then your definition of "abstract" must be pretty strange. Ofc you can make tricks in C++ with friends and instanciate a class with a protected or private constructor. Nevertheless literature considers such a class abstract. If you don't have virtual functions, the class is not polymorphic anyway. But I assume in your case you mean assignable-polymorphism, the only thing all derived objects have in common is that you can "delete" them.Orelee
@AngelO'Sphere: There's nothing to be confused about. In C++ a class is "abstract" if it has pure-virtual functions or unoverridden pure-virtual base functions, period.Caril
According to whose definition? A pure virtual function is only one way to make a class abstract. Abstractness is an OO term, not an C++ term. The class is also abstract if it has protected ctors. The more confusing thing or question towards Mike was: why does he think a class with a pure virtual destructor is polymorph? To be polymorph imho a class needs to define at least one virtual function, regardless whether pure or not. Strictly speaking the function does not even need to be virtual (e.g. if the class is mainly used as template argument).Orelee
@AngelO'Sphere: This question is about C++, where the meanings of "abstract class" and "polymorphic class" are defined by the language standard as, respectively, "a class with at least one pure virtual function" and "a class with at least one virtual function". Other meanings of those words might be useful in other contexts, but just cause confusion when used to describe other C++ concepts.Manx
Ah, I had expected those where explanaitions and not definitions. Thanx for the clarification.Orelee
E
11

are there any situations when it is beneficial to define a pure virtual function in the base class itself?

Yes - if the function in question is the pure virtual destructor, it must also be defined by the base class.

Exertion answered 9/11, 2011 at 11:12 Comment(1)
Thanks, any other situations?Pinup
W
0

A base-class with only pure virtual functions are what languages like Java would call an interface. It simply describes what functions are available, nothing else.

Waterresistant answered 9/11, 2011 at 11:1 Comment(3)
Except, in C++, pure virtual functions can have implementations. Hence the question, when would you want to give them implementations?Manx
@MikeSeymour If a virtual function has an implementation, it is no longer pure. The base (interface) class have pure virtual functions, while the derived classes have normal virtual function.Waterresistant
A virtual function is pure if it is declared so (i.e. followed by =0). This makes the class abstract (preventing instantiation except as a base class), but does not say anything about whether or not the function has an implementation. As I said, pure virtual functions can have implementations.Manx
P
0

It can be beneficial when there is no reasonable implementation of pure virtual function can be in base class. In this case pure virtual functions are implemented in derived classes.

Pivoting answered 9/11, 2011 at 11:2 Comment(2)
Except the question is, when would it be beneficial to implement them in the base class.Manx
@Mike Seymour: it is beneficial to implement some default behavior in base class to avoid code duplication for examplePivoting

© 2022 - 2024 — McMap. All rights reserved.