473,507 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why virtual constructor

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

Oct 4 '05 #1
5 2013
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

Oct 4 '05 #2
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
kb******@gascad.at
Oct 5 '05 #3
thx i got it...

Devika

Nov 2 '05 #4

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.

Nov 2 '05 #5
thx a lot for the detailed explanation..it helped a lot

Devika

Nov 3 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
4241
by: Lefevre | last post by:
Hello I recently had troubles with a class inheritance hierarchy. I solved it, but it didn't satisfied me. I found the solution using this forum :) Actualy i found the following message...
11
10494
by: Stub | last post by:
Please answer my questions below - thanks! 1. Why "Derived constructor" is called but "Derived destructor" not in Case 1 since object B is new'ed from Derived class? 2. Why "Derived destructor"...
6
2609
by: Thomas Matthews | last post by:
Hi, Is placing the keyword "virtual" in front of a constructor allowed as in the sample below? class TTable { virtual TTable(); }; My compiler, Borland Builder 5.2, has system libraries
3
5156
by: ccs | last post by:
In Meyers' book he gave an example of "virtual copy constructor", which is quite different to an "ordinary" copy constructor by: 1. it returns a pointer to an object instead of a reference. 2. it...
22
2920
by: Ruben Van Havermaet | last post by:
Hi, I have a problem using member functions in derived classes that override virtual member functions of base classes. The following pieces of (simplified) code don't work. Can anybody give...
16
3721
by: plmanikandan | last post by:
Hi, I have doubts reg virtual constructor what is virtual constructor? Is c++ supports virtual constructor? Can anybody explain me about virtual constructor? Regards, Mani
4
2331
by: Stefan Nikolaus | last post by:
Hello, I've run into problems with defining a template, which inherits from a base template and the usage of virtual methods in those. I want to initialize a member variable depending on which...
3
2341
by: Klaas Vantournhout | last post by:
Hi, Recently I obtained a problem with virtual inheritance when implementing it in multiple files. To present the problem I have included at the bottom of this post the code of the 4 files. I...
1
3455
by: Bart Simpson | last post by:
Can anyone explain the concept of "slicing" with respect to the "virtual constructor" idiom as explain at parashift ? From parashift: class Shape { public: virtual ~Shape() { } ...
17
3510
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
0
7221
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7109
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7313
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7029
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3190
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1537
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.