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?
With one step more you could just introduce some kind of a function like
Title of the following article says it all: Never Call Virtual Functions during Construction or Destruction.
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:
Example 1: runtime binding
Example 2: compiletime binding
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:
You need to wrap in inside an object that calls the virtual method after the object is fully constructed:
Alternatively, make a factory method for creating the objects and make the constructors private, the factory method can then Initialize the object after construction.