Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Parent class calling a child virtual function

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?

like image 542
Whaledawg Avatar asked Oct 23 '08 23:10

Whaledawg


People also ask

How do you call a child function from a parent class?

One way to call a child component's function from its parent is with the help of the useRef hook. Here's the gist of it: We pass a Ref (e.g. childFunc ) to a child 🧒 component and assign the desired function to the Ref's current key. This then gives you access to that very function in the parent component.

How do you call an inherited class function?

A derived class is created, which is inheriting parent class p1 and overloading the parent class function first(). class d1 : public p1 { public: void first() { cout << "The derived class d1 function is called."; p1::first(); } }; The function of d1 class is calling the function of p1 class.

Can you inherit virtual function?

Base classes can't inherit what the child has (such as a new function or variable). Virtual functions are simply functions that can be overridden by the child class if the that child class changes the implementation of the virtual function so that the base virtual function isn't called. A is the base class for B,C,D.

How the function of multiple parent classes can be called from a derived class?

If there is a function with the same signature in the derived class you can disambiguate it by adding the base class's name followed by two colons base_class::foo(...) .


2 Answers

The superficial problem is that you call a virtual function that's not known yet (Objects are constructed from Parent to Child, thus so are the vtables). Your compiler warned you about that.

The essential problem, as far as I can see, is that you try to reuse functionality by inheritance. This is almost always a bad idea. A design issue, so to speak :)

Essentially, you try instantiating a Template Method pattern, to separate the what from the when: first read some data (in some way), then process it (in some way).

This will probably much better work with aggregation: give the Processing function to the Template method to be called at the right time. Maybe you can even do the same for the Read functionality.

The aggregation can be done in two ways:

  1. Using virtual functions (i.e. Runtime Binding)
  2. Using templates (i.e. Compile Time Binding)

Example 1: runtime binding

class Data {};
class IReader    { public: virtual Data read()            = 0; };
class IProcessor { public: virtual void process( Data& d) = 0; };

class ReadNProcess {
public:
    ReadNProcess( IReader& reader, IProcessor processor ){
       processor.process( reader.read() );
    }
};

Example 2: compiletime binding

template< typename Reader, typename Writer > // definitely could use concepts here :)
class ReadNProcess {
public:
     ReadNProcess( Reader& r, Processor& p ) {
         p.process( r.read() );
     }
};
like image 124
xtofl Avatar answered Sep 20 '22 18:09

xtofl


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

like image 37
Damir Zekić Avatar answered Sep 21 '22 18:09

Damir Zekić