I would like a pure virtual parent class to call a young child implementation of the function like so:
class parent virtual void Process() = parent() class child : public parent child() : parent() int primary()
This will work, however i have an unlinked error :/ This really is using VC++ 2k3
Or should not it work, am I wrong?
Title from the following article states everything: Never Call Virtual Functions throughout Construction or Destruction.
Alternatively, create a factory way of creating the objects making the constructors private, the factory method may then Initialize the item after construction.
Works generally, although not for calls inside the constructor from the pure virtual base class. At that time the bottom class in built, the sub-class override does not exist, which means you can't refer to it as. As lengthy while you refer to it as when the entire object is built, it will work.
It is because your call is incorporated in the constructor. The derived class won't be valid before the constructor has completed which means you compiler is appropriate in dinging you with this.
You will find two solutions:
- Result in the call to Process() within the derived class's constructor
- define an empty function body for Process as with the next example:
class parent virtual void Process() parent()
With a measure more you can just introduce some type of a function like
You have to wrap in in a object that calls the virtual method following the object is fully built:
class parent virtual void Process() = parent() class child: public parent child() : parent() template<typename T> class Processor private: T processorObj int primary()
The superficial problem is you call an online function that isn't known yet (Objects are built from Parent to Child, thus so might be the vtables). Your compiler cautioned you about this.
The essential problem, so far as I can tell, is you attempt to reuse functionality by inheritance. This really is more often than not an awful idea. A design problem, as they say :)
Basically, you attempt instantiating a Template Method pattern, to split up the what in the when: first read some data (in some manner), then process it (in some manner).
This can most likely far better use aggregation: provide the Processing function towards the Template approach to be known as in the proper time. Maybe you may also perform the same for that Read functionality.
The aggregation could be completed in two ways:
- Using virtual functions (i.e. Runtime Binding)
- Using templates (i.e. Compile Time Binding)
Example 1: runtime binding
class Data class IReader class IProcessor amplifier d) = class ReadNProcess amplifier readers, IProcessor processor )readers.read() )
Example 2: compiletime binding
template< typename Readers, typename Author > // certainly can use concepts here :) class ReadNProcess Readers&lifier r, Processor&lifier p )