Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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


Please Help Members By Posting Answers For Below Questions

You run a shell on unix system. How would you tell which shell are you running?

1164


What is the difference between passing by reference and passing a reference?

1076


Is map ordered c++?

1089


Why is "using namespace std;" considered bad practice?

1092


What is the full form nasa?

1091


What is new in c++?

1098


How many characters are recognized by ANSI C++?

1418


How can I improve my c++ skills?

1127


Which format specifier is used for printing a pointer value?

1051


How many different levels of pointers are there?

1205


What do you mean by ‘void’ return type?

1052


Will a recursive function without an end condition every quit, in practice a) Compiler-Specific (Some can convert to an infinite loop) b) No c) Yes

1071


Is c++ used anymore?

1088


What is function overriding in c++?

1061


How is new() different from malloc()?

1117