Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Pure Virtual Function From Abstract Base Class Member Function?

So, based on a cursory search, I already know that calling a virtual function (pure or otherwise) from a constructor is a no go. I have restructured my code to ensure that I am not doing that. While this causes the user of my classes to add an extra function call in their code, this is really not that big of a deal. Namely, instead of calling the constructor in a loop, they now call the function which (in fact!) increases performance of the code since we don't have the housekeeping of building and destroying the object in question every time.

However, I have stumbled across something interesting...

In the abstract class I have something like this:

// in AbstractClass.h:
class AbstractClass {
 public:
  AbstractClass() {}
  virtual int Func(); //user can override
 protected:
  // Func broken up, derived class must define these
  virtual int Step1() = 0;
  virtual int Step2() = 0;
  virtual int Step3() = 0;
// in AbstractClass.cpp:
int AbstractClass::Func() {
  Step1();
  // Error checking goes here
  Step2();
  // More error checking...
  // etc...
}

Basically, there is a common structure that the pure virtual functions follow most of the time, but if they don't Func() is virtual and allows the derived class to specify the order. However, each step must be implemented in derived classes.

I just wanted to be sure that there's nothing that I necessarily am doing wrong here since the Func() function calls the pure virtual ones. That is, using the base class, if you call StepX(), bad things will happen. However, the class is utilized by creating a derived object and then calling Func() (e.g. MyDerivedObject.Func();) on that derived object, which should have all the pure virtual functions overloaded properly.

Is there anything that I'm missing or doing incorrectly by following this method? Thanks for the help!

like image 522
It'sPete Avatar asked Jul 02 '13 01:07

It'sPete


People also ask

Can you call a pure virtual function in base class?

Pure virtual functions must not be called from a C++ constructor. As a general rule, you should never call any kind of virtual function in a constructor or destructor because those calls will never go to a more derived class than the currently executing constructor or destructor.

Is it legal to have an abstract class with all member functions pure virtual?

Pure Virtual definitions Still you cannot create object of Abstract class. Also, the Pure Virtual function must be defined outside the class definition. If you will define it inside the class definition, complier will give an error. Inline pure virtual definition is Illegal.

How do you call a virtual function through a base class reference?

A virtual function is a member function that you expect to be redefined in derived classes. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class's version of the function.

Can abstract class have virtual function?

An abstract class is a class that is designed to be specifically used as a base class. An abstract class contains at least one pure virtual function. You declare a pure virtual function by using a pure specifier ( = 0 ) in the declaration of a virtual member function in the class declaration.


2 Answers

Func is calling the virtual ones, not the pure virtual ones. You would have to qualify the calls with a scope operator, i.e. AbstractClass::Step1() to call THAT (virtual pure) function. Since you are not, you will always get an implementation by a derived class.

like image 57
franji1 Avatar answered Sep 17 '22 03:09

franji1


A virtual function in a base class makes the derived classes able to override it. But it seems things stop there.

But if the base class virtual function is pure, that forces the derived classes to implement the function.

like image 34
Azeem Ansari Avatar answered Sep 17 '22 03:09

Azeem Ansari