ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
tip   SiteMap shows list of All Categories in this site.
Google
 
Categories >> Software >> Programming-Languages >> C++ >> C++-General
 
 
 
Question
How to construct virtual constructor
 Question Submitted By :: Shalini
I also faced this Question!!     Rank Answer Posted By  
 
Answer
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.
 
0
Mmszubeir
 
View All Answers
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com