Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old March 28th, 2006, 05:45 PM
vaividhya@gmail.com
Guest
 
Posts: n/a
Default virtual functions

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

  #2  
Old March 28th, 2006, 05:45 PM
Tomás
Guest
 
Posts: n/a
Default Re: virtual functions

vaividhya@gmail.com posted:
[color=blue]
> We can have virtual destructors.Why we can't have virtual constructors?[/color]


'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
  #3  
Old March 28th, 2006, 06:35 PM
Rolf Magnus
Guest
 
Posts: n/a
Default Re: virtual functions

vaividhya@gmail.com wrote:
[color=blue]
> We can have virtual destructors.Why we can't have virtual constructors?[/color]

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.

  #4  
Old March 28th, 2006, 06:55 PM
Tejas Kokje
Guest
 
Posts: n/a
Default Re: virtual functions

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

  #5  
Old March 28th, 2006, 08:05 PM
Phlip
Guest
 
Posts: n/a
Default Re: virtual functions

Rolf Magnus wrote:
[color=blue]
> vaividhya@gmail.com wrote:
>[color=green]
>> We can have virtual destructors.Why we can't have virtual constructors?[/color]
>
> 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.[/color]

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!!!
  #6  
Old March 28th, 2006, 08:25 PM
Alf P. Steinbach
Guest
 
Posts: n/a
Default Re: virtual functions

* Phlip:[color=blue]
> Rolf Magnus wrote:
>[color=green]
>> vaividhya@gmail.com wrote:
>>[color=darkred]
>>> We can have virtual destructors.Why we can't have virtual constructors?[/color]
>> 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.[/color]
>
> That's not "why", it just states what the current mechanism is.[/color]

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.

[color=blue]
> 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.[/color]

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.

[color=blue]
> 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.[/color]

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>.

[color=blue]
> The work-around might be "Coplien's Curiously Recurring Template Pattern".[/color]

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?
  #7  
Old March 28th, 2006, 08:45 PM
Phlip
Guest
 
Posts: n/a
Default Re: virtual functions

Alf P. Steinbach wrote:
[color=blue]
> But this is something very different from calling a constructor virtually.
>
> Instead, it's all about virtual calls from a constructor.[/color]

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!!!
  #8  
Old March 29th, 2006, 09:15 AM
chakresh
Guest
 
Posts: n/a
Default Re: virtual functions

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

 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles