C++ Parent class calling a child virtual function

17,243

Solution 1

Title of the following article says it all: Never Call Virtual Functions during Construction or Destruction.

Solution 2

Alternatively, make a factory method for creating the objects and make the constructors private, the factory method can then Initialize the object after construction.

Solution 3

Will work in general, but not for calls within the constructor of the pure virtual base class. At the time the base class in constructed, the sub-class override doesn't exist, so you can't call it. As long as you call it once the entire object is constructed, it should work.

Solution 4

It's because your call is in the constructor. The derived class will not be valid until the constructor has completed so you compiler is right in dinging you for this.

There are two solutions:

  1. Make the call to Process() in the derived class's constructor
  2. define a blank function body for Process as in the following example:
class parent
{
  public:
    void Read() { //read stuff }
    virtual void Process() { }
    parent() 
    {
        Read();
        Process();
    }
}

Solution 5

With one step more you could just introduce some kind of a function like

class parent
{
    public:
        void initialize() {
            read();
            process();
        }
}
Share:
17,243
Whaledawg
Author by

Whaledawg

Updated on June 14, 2022

Comments

  • Whaledawg
    Whaledawg almost 2 years

    I want a pure virtual parent class to call a child implementation of a function like so:

    class parent
    {
      public:
        void Read() { //read stuff }
        virtual void Process() = 0;
        parent() 
        {
            Read();
            Process();
        }
    }
    class child : public parent
    {
      public:
        virtual void Process() { //process stuff }
        child() : parent() { }
    }
    
    int main()
    {
       child c;
    }
    

    This should work, but I get an unlinked error :/ This is using VC++ 2k3

    Or shouldn't it work, am I wrong?

  • Pieter
    Pieter over 15 years
    This is dangerous, defining a blank function body in the parent and calling it in its constructor will result in only the parent part of Process() (i.e. nothing) being executed. He likely wants the call to be resolved as a virtual function, that's impossible in the constructor
  • xtofl
    xtofl over 15 years
    Indeed: the blank function will be called. That's no viable solution.
  • paercebal
    paercebal over 15 years
    Correct, but somewhat short. You should at least copied the conclusion: "Things to Remember: Don't call virtual functions during construction or destruction, because such calls will never go to a more derived class than that of the currently executing constructor or destructor."
  • xtofl
    xtofl over 15 years
    Still, I really think you should think twice before mixing in functionality by inheritance. IMHO, this question shows a design flow under the surface.