473,396 Members | 2,011 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,396 software developers and data experts.

virtual functions

We can have virtual destructors.Why we can't have virtual constructors?

Mar 28 '06 #1
7 2536
va*******@gmail.com posted:
We can have virtual destructors.Why we can't have virtual constructors?

'Cause they'd be redudant. A constructor is called once: upon
construction of the object. An object is contructed as follows:

TypeName object_name;

Therefore, the compiler knows straight away which contructor should be
called, and therefore need not consult any v-table.

-Tomás
Mar 28 '06 #2
va*******@gmail.com wrote:
We can have virtual destructors.Why we can't have virtual constructors?


Because for virtual functions (including the destructor), the system
determins the dynamic type of the object to find out which function to call
at runtime. But a constructor is called to create the object, so there is
no dynamic type to find out yet.

Mar 28 '06 #3
We have virtual destructors because of run time polymorphism. Consider
following example,

class Base {

char *p;

Base() {
p = new char[20];
}

~Base() {

delete[] p;

}

};

class Derived: public Base {

char *t;

Derived() {
t = new char[20];
strcpy (t,p);
}

~Derived() {
delete[] t;
}

};

Now if you declare a Base class pointer

Base *b = new Derived();

and then do
delete b;

it will call Base class destructor instead of derived class destructor
since Base class desctructor is not virtual. This will keep memory
allocated to t and never free up. Also, if p is being used by derived
class, it will be freed by Base class destructor before derived class
is destroyed.

If we declare Base class destructor as virtual, then derived class
destructor will be called first and then Base class destructor.

As for constructor not being virtual, you would not want to use p in
derived class before it is allocated in Base class. Base class
constructor should be called first and then derived class.

Hope this helps.

Tejas Kokje
Code Monkey
http://mailmount.sf.net

Mar 28 '06 #4
Rolf Magnus wrote:
va*******@gmail.com wrote:
We can have virtual destructors.Why we can't have virtual constructors?


Because for virtual functions (including the destructor), the system
determins the dynamic type of the object to find out which function to
call at runtime. But a constructor is called to create the object, so
there is no dynamic type to find out yet.


That's not "why", it just states what the current mechanism is.

The compiler always knows the type of the fully-formed object at
construction time. So C++ could have been defined as constructing the
complete virtual method jump table first, instead of last. Then
constructors could freely call virtual methods into the derived classes,
and the result would be virtual construction.

The reason we can't upgrade the current language to support that feature is
too much code depends on constructors calling virtual methods that don't
dispatch to their derived overrides. The work-around might be "Coplien's
Curiously Recurring Template Pattern".

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Mar 28 '06 #5
* Phlip:
Rolf Magnus wrote:
va*******@gmail.com wrote:
We can have virtual destructors.Why we can't have virtual constructors? Because for virtual functions (including the destructor), the system
determins the dynamic type of the object to find out which function to
call at runtime. But a constructor is called to create the object, so
there is no dynamic type to find out yet.


That's not "why", it just states what the current mechanism is.


Right you are, so far.

In at least one situation the C++ abstract machine is capable of finding
a relevant static function based on an object's dynamic type (namely, a
deallocation function, 'operator delete'), and in like manner the
language could technically have supported virtual constructors, either
with some run-time checking of argument types, or corresponding checking
of an assumed signature against the actual dynamically found signature,
or simply requiring some fixed, generic signature.

The compiler always knows the type of the fully-formed object at
construction time. So C++ could have been defined as constructing the
complete virtual method jump table first, instead of last. Then
constructors could freely call virtual methods into the derived classes,
and the result would be virtual construction.
In one sense of "virtual construction", yes: that's the way it works in
Java and C#.

But this is something very different from calling a constructor virtually.

Instead, it's all about virtual calls from a constructor.

The reason we can't upgrade the current language to support that feature is
too much code depends on constructors calling virtual methods that don't
dispatch to their derived overrides.
No, the reason that we can't downgrade to the Java/C# model is that we
want to preserve type safety, where type safety includes the notion of
class invariants; see the FAQ at <url:
http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.5>.

The reason why we, in practice, can't upgrade C++ to directly support
virtual constructors, virtual calls of constructors, is that this would
extend the language a great deal for something that can easily be
achieved within the language as-is, namely via clone functions.

For those who are not familiar with clone functions, see the FAQ, <url:
http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.8>.

The work-around might be "Coplien's Curiously Recurring Template Pattern".


That's one of several work-arounds for implementing something akin to
the Java/C# model; see the FAQ at <url:
http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.6>.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 28 '06 #6
Alf P. Steinbach wrote:
But this is something very different from calling a constructor virtually.

Instead, it's all about virtual calls from a constructor.


Correct. I'm playing language inventor. I tell my teeming minions that y'all
can't get virtual constructors, but I will give you virtual method calls
from within constructors, because they are just as useful. And per your FAQ
citation, they are indeed dangerous, but that never stopped C++ before. And
the FAQ citation doesn't say why we _can't_ downgrade to the Java system,
just why we won't.

Also The Standard sez that constructors are "not functions", so I don't want
to go against whatever else that implies. Subconstructor notation,
try{}catch wrappers, and who knows what else.

So the ultimate "why" is probably going to be "because" here...

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Mar 28 '06 #7
I am unable to undestand why we need virtual constructor.
As when we create some object we know the type of object and call that
constructor so why need virtual const.
In decst..case we can have a case where derived object is pointed by a
pointer of base class and we use delete to release memory. and if it
would be virtual distructor then only it can call proper derivced class
const...

Corect me if I am wrong

Mar 29 '06 #8

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

Similar topics

4
by: vijay | last post by:
I have a doubt with size of classed with virtual functions I have declared A,A1,A2 ,B , C, D some classes with no varaibles but a vitual function each, The size of A is as expected 4 bytes with...
5
by: Ryan Faulkner | last post by:
Hi, Im having a few problems with virtual functions (Im using the Visual C++ environment by the way). I have a base class with three virtual functions and a derived class with a single new...
18
by: nenad | last post by:
Wouldn't it be nice if we could do something like this: class Funky{ public: auto virtual void doStuff(){ // dostuff } };
19
by: qazmlp | last post by:
class base { // other members public: virtual ~base() { } virtual void virtualMethod1()=0 ; virtual void virtualMethod2()=0 ; virtual void virtualMethod3()=0 ;
7
by: Aguilar, James | last post by:
I've heard that virtual functions are relatively ineffecient, especially virtual functions that are small but get called very frequently. Could someone describe for me the process by which the...
25
by: Stijn Oude Brunink | last post by:
Hello, I have the following trade off to make: A base class with 2 virtual functions would be realy helpfull for the problem I'm working on. Still though the functions that my program will use...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
7
by: ashishnh33 | last post by:
i want to know about the concept behind the virtual functions.
5
by: Dilip | last post by:
In a polymorphic hierarchy is it common practice to have public virtual functions in the base class with a default empty implementation and let the derived classes decide whether they want to...
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.