Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > why virtual constructor

Reply
Thread Tools

why virtual constructor

 
 
Devika
Guest
Posts: n/a
 
      10-04-2005
hi,

I understand the need of virtual destructor.but virtual constructor is
not supported in c++.but basically why we need to have virtual
constructor??

thx in advance

Devika

 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      10-04-2005
Devika wrote:
> hi,
>
> I understand the need of virtual destructor.but virtual constructor is
> not supported in c++.but basically why we need to have virtual
> constructor??
>
> thx in advance
>
> Devika


See the FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-20.8

Cheers! --M

 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      10-05-2005
Devika wrote:
>
> hi,
>
> I understand the need of virtual destructor.but virtual constructor is
> not supported in c++.but basically why we need to have virtual
> constructor??


Because sometimes you have a pointer to an object. You don't know
which object exactly, but you do know that it is somehow derived
from some baseclass. And now you want a copy of that object. How
would you do that.

eg.
You have geometric shapes (aka a graphical editor). Now you
want to implement cut&paste operation. For this you need to be able
to generate copies of the selected shapes. But all you have are a bunch
of shape pointers, but you don't know if these pointers point to
circles, lines, splines, areas or texts.

--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Devika
Guest
Posts: n/a
 
      11-02-2005
thx i got it...

Devika

 
Reply With Quote
 
Kaz Kylheku
Guest
Posts: n/a
 
      11-02-2005

Devika wrote:
> hi,
>
> I understand the need of virtual destructor.but virtual constructor is
> not supported in c++.but basically why we need to have virtual
> constructor??


It makes no sense for a C++ constructor to be virtual, because the
exact type of an object is statically obvious at compile time. Based on
the declared, manifest type, the compiler knows exactly what
constructors have to be called and in what order.

A virtual destructor is needed so you can destroy an object through a
base class pointer. You invoke ``delete b;'' on a ``Base *b'' which
might actually be pointer to a Derived. The compiled code has to
properly destroy that object regardless of its type, and the virtual
destructor mechanism does that.

There is sometimes a need to construct an object whose type is not
known. For example, suppose you have some program that lets the user to
enter a class name as a string, like "wizard" or "warrior". You parse
the string, and based on what is parsed, you have to construct
different types of objects.

There are a number of ways to do that kind of thing. One way is the
Abstract Factory pattern. You use the run-time data (string or
whatever) as a key to fetch a factory from dictionary of factories.
Then you call the virtual functions on the factory to make objects of
that factory's type for you.

You may also need a mechanism to deal with construction parameters. The
factory interface may have to take some kind of abstract list of
keyword-value pairs. Each implementation of that interface will have to
parse from that structure the things it knows about, perform the
appropriate conversions, and then invoke the constructor with the right
arguments. Or perhaps internally choose from among several different
overloads of the constructor.

 
Reply With Quote
 
Devika
Guest
Posts: n/a
 
      11-03-2005
thx a lot for the detailed explanation..it helped a lot

Devika

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
why virtual constructor Devika C++ 0 10-04-2005 05:46 PM
why it's not possible calling constructor from constructor? Giulio C++ 9 06-25-2003 03:56 PM



Advertisments