How to construct virtual constructor
Answer Posted / mmszubeir
C++ doesn’t give any direct support to declare a virtual
constructor as such. But we can have a virtual constructor
by indirectly providing the mechanism.
As like ordinary virtual functions, a virtual constructor
can exhibit different behaviors depending upon the request.
That is, a virtual constructor can construct different
types of objects on different requests. This implies the
class hierarchy should be polymorphical.
This technique resembles two stage construction technique.
To avoid breaking at the constructor of an object, we keep
the heavy initialization code in a separate initializer
function so that we get the minimum guarantee that the
constructor won’t fail.
In this case, we first invoke the constructor and then
invoke the initializer function. The advantage of this
method is that we will have a better control of the
construction.
For example,
CBrush blueBrush; // Just an object. Must be initialized!
blueBrush.CreateSolidBrush(RGB(0,0,255)); // Blue brush.
Since I am running short of time, I hang on with this for
the time being. I will add more details about the virtual
constructor theory soon.
Is This Answer Correct ? | 10 Yes | 5 No |
Post New Answer View All Answers
Is java based off c++?
Explain what are the sizes and ranges of the basic c++ data types?
How do you find out if a linked-list has an end? (I.e. The list is not a cycle)
What is difference between c++ and c ++ 14?
Can a program run without main function?
Explain deep copy and a shallow copy?
Explain all the C++ concepts using examples.
What is object oriented programming (oop)?
What does flush do c++?
What is class definition in c++ ?
What are the differences between the function prototype and the function defi-nition?
Which of the following is not a valid declaration for main() a) int main() b) int main(int argc, char *argv[]) c) They both work
What is a class definition?
Will rust take over c++?
When should we use container classes instead of arrays?