C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

why virtual constructor

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Devika
Guest





PostPosted: Tue Oct 04, 2005 5:46 pm    Post subject: why virtual constructor Reply with quote



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

Back to top
mlimber
Guest





PostPosted: Tue Oct 04, 2005 5:54 pm    Post subject: Re: why virtual constructor Reply with quote



Devika wrote:
Quote:
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-lite/virtual-functions.html#faq-20.8

Cheers! --M


Back to top
Karl Heinz Buchegger
Guest





PostPosted: Wed Oct 05, 2005 8:33 am    Post subject: Re: why virtual constructor Reply with quote



Devika wrote:
Quote:

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
[email]kbuchegg (AT) gascad (DOT) at[/email]

Back to top
Devika
Guest





PostPosted: Wed Nov 02, 2005 4:39 pm    Post subject: Re: why virtual constructor Reply with quote

thx i got it...

Devika

Back to top
Kaz Kylheku
Guest





PostPosted: Wed Nov 02, 2005 6:01 pm    Post subject: Re: why virtual constructor Reply with quote


Devika wrote:
Quote:
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.


Back to top
Devika
Guest





PostPosted: Thu Nov 03, 2005 10:24 pm    Post subject: Re: why virtual constructor Reply with quote

thx a lot for the detailed explanation..it helped a lot

Devika

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.