How to construct virtual constructor

Answers were Sorted based on User's Feedback



How to construct virtual constructor..

Answer / richa

Constructors can't be virtual in C++.It is an error.The
destructors on the other hand can be virtual.

Is This Answer Correct ?    29 Yes 4 No

How to construct virtual constructor..

Answer / shakti singh khinchi

Virtual constructors are not used in any case in c++, bcz
the construction order in c++ should always remains from
base to derived , no one can changed their construction
order, so no need to declare constructor as virtual. But if
anyone wants to make it virtual then it is possible to make
a constructor virtual but it is against the OOPs.

Is This Answer Correct ?    12 Yes 2 No

How to construct virtual constructor..

Answer / 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

How to construct virtual constructor..

Answer / nishant

Firstly Virtual constructor is really not needed at all.
Explanation:
V-table is something which stores the addresses corrospond
to all virtual function within a class.Now every class
object contains virtual pointer points to base address of
v-table in memory which is being used to resolve the
corrosponding virtual function addresses.

This v-table construction is done when object of the class
has been created,While constructor is an initialization
process in construction of the object and so v-table is not
available during initialization process and hence virtual
constructor is not possible.

Is This Answer Correct ?    4 Yes 0 No

How to construct virtual constructor..

Answer / vishwa

there is no need to use vitual constructors in C++.
because the construction of the class tree will be happened
from the base class to child class.

Is This Answer Correct ?    9 Yes 9 No

How to construct virtual constructor..

Answer / gaurav

The first thing, Why we need a Virtual constructors, do we
really need that..... :) If I am making an object of A then
I should not expect the object of class B as I can directly
make a B class object.

Is This Answer Correct ?    1 Yes 4 No

Post New Answer

More C++ General Interview Questions

What is the output of the following 3D Array int arr[3][2][2]={1,2,3,4,5,6,7,8,9,10,11,12}; what is the output for arr[2][1][0]?

6 Answers   HCL, Integra, IPMC, ORG,


pls help.. paper bills.. 1000, 500, 100, 50, 20, 10, 5, 1.. create a program that will count all the paper bills in the number being input.. example: enter a number: 3886 there is/are: 3 ->1000 1 ->500 3 ->100 1 ->50 1 ->20 1 ->10 1 ->5 1 ->1 example2: enter a number: 728 there is/are: 0 ->1000 1 ->500 2 ->100 0 ->50 1 ->20 0 ->10 1 ->5 3 ->1

4 Answers  


Is c++ fully object oriented?

0 Answers  


Is eclipse good for c++?

0 Answers  


What is a storage class? Mention the storage classes in c++.

0 Answers  






Is ca high or low level language?

0 Answers  


Is c++ an oop?

0 Answers  


What is guard code in c++?

0 Answers  


What does floor mean in c++?

0 Answers  


What does h mean in maths?

0 Answers  


sizeof- is it functioning statically or dynamically?

2 Answers  


Can we remove an element in a single linked list without traversing? Lets suppose the link list is like this 1 2 3 4 5 6 We need to remove 4 from this list (without traversing from beginning) and the final link list shud be 1 2 3 5 6 only thing we know is the pointer to element "4". How can we remove "4" and link "3" to "5"?

6 Answers   CSC,


Categories