[ fromfile: factory.xml id: factorybenefits ]
One of the benefits of factory patterns is that we can manage the created objects in a pool (reusing the ones that can be reused).
Indirect object creation also makes it possible to decide at runtime which class objects to create.
This enables the "plugging in" of replacement classes without requiring changes in the client code.
In src/libs/metadata/abstractmetadataloader.h
, there is another example of a factory method that manages singleton instances of MetaDataLoader in such a way that it becomes easy to write programs that can switch between derived metadata loaders without code breakage.
including the invocation of virtual functions.
An object is not considered "fully constructed" until the constructor has finished executing.
An object's vpointer does not point to the correct vtable until the end of the constructor's execution.
Therefore, calls to methods of this
from the constructor cannot use polymorphism!
Example 16.8 demonstrates this problem.
Example 16.8. src/ctorpoly/ctorpoly.cpp
#include <iostream> using namespace std; class A { public: A() { cout << "in A ctor" << endl; foo(); } virtual void foo() { cout << "A's foo()" << endl; } }; class B: public A { public: B() { cout << "in B ctor" << endl; } void foo() { cout << "B's foo()" << endl; } }; class C: public B { public: C() { cout << "in C ctor" << endl; } void foo() { cout << "C's foo()" << endl; } }; int main() { C* cptr = new C; cout << "After construction is complete:" << endl; cptr->foo(); return 0; }
Its output is given in Example 16.9.
Example 16.9. src/ctorpoly/ctorpoly-output.txt
src/ctorpoly> ./a.out in A ctor A's foo() in B ctor in C ctor After construction is complete: C's foo() src/ctorpoly>
Section 22.1 discusses vtables in more detail.
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |