473,320 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 2004
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
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
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
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
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
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
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
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
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
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
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
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.