Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > virtual functions

Reply
Thread Tools

virtual functions

 
 
rajesh kalra
Guest
Posts: n/a
 
      05-28-2008
do the virtual mechanism works when virtual functions are called in
constructors?
 
Reply With Quote
 
 
 
 
Barry
Guest
Posts: n/a
 
      05-28-2008
rajesh kalra wrote:
> do the virtual mechanism works when virtual functions are called in
> constructors?


Never do it!
http://www.artima.com/cppsource/nevercall.html

--
Best Regards
Barry
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      05-28-2008
rajesh kalra wrote:
> do the virtual mechanism works when virtual functions are called in
> constructors?


Yes, but take care if you do!

The function called is the one in the class under construction, or one
of its base classes (12.7.3). If a function is pure virtual, it can't
be called unless it has been overridden.

--
Ian Collins.
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      05-28-2008
Barry wrote:
> rajesh kalra wrote:
>> do the virtual mechanism works when virtual functions are called in
>> constructors?

>
> Never do it!
> http://www.artima.com/cppsource/nevercall.html


Do Java and C# call the derived implementations of virtual functions
even though the derived part has yet not been constructed (its
constructor has not been called yet)?
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      05-28-2008
Ian Collins wrote:
> rajesh kalra wrote:
>> do the virtual mechanism works when virtual functions are called in
>> constructors?

>
> Yes, but take care if you do!


Curiously, it's even possible to call base class functions *before*
the base class part of the object has been constructed at all, using a
small trick (which even more curiously is legal C++, although of course
extremely not recommended).
 
Reply With Quote
 
Barry
Guest
Posts: n/a
 
      05-28-2008
Juha Nieminen wrote:
> Barry wrote:
>> rajesh kalra wrote:
>>> do the virtual mechanism works when virtual functions are called in
>>> constructors?

>> Never do it!
>> http://www.artima.com/cppsource/nevercall.html

>
> Do Java and C# call the derived implementations of virtual functions
> even though the derived part has yet not been constructed (its
> constructor has not been called yet)?


Yes, both


--
Best Regards
Barry
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      05-28-2008
On May 28, 10:41 am, Juha Nieminen <(E-Mail Removed)> wrote:
> Barry wrote:
> > rajesh kalra wrote:
> >> do the virtual mechanism works when virtual functions are called in
> >> constructors?


> > Never do it!
> >http://www.artima.com/cppsource/nevercall.html


> Do Java and C# call the derived implementations of virtual
> functions even though the derived part has yet not been
> constructed (its constructor has not been called yet)?


Don't know about C#, but Java does. It can easily be a source
of strange errors, too.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
ManicQin
Guest
Posts: n/a
 
      05-29-2008
On May 28, 11:43 am, Juha Nieminen <(E-Mail Removed)> wrote:
> Ian Collins wrote:
> > rajesh kalra wrote:
> >> do the virtual mechanism works when virtual functions are called in
> >> constructors?

>
> > Yes, but take care if you do!

>
> Curiously, it's even possible to call base class functions *before*
> the base class part of the object has been constructed at all, using a
> small trick (which even more curiously is legal C++, although of course
> extremely not recommended).


Can you post the trick (or refer to it)?
Just for the fun of it.
Thanks
 
Reply With Quote
 
 
 
Reply

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
private virtual functions and pure virtual functions with bodies John Goche C++ 10 12-08-2006 04:00 PM
virtual functions in virtual base class sreelakshmi.rajula@gmail.com C++ 7 08-28-2006 09:29 PM
calling virtual destructors and virtual functions marcwentink@hotmail.com C++ 3 01-31-2006 04:50 PM
virtual destructors for classes only with virtual functions? heted7 C++ 33 05-12-2005 09:26 AM
Virtual functions and virtual base classes - I'm confused Michael Winter C++ 9 09-23-2003 11:17 PM



Advertisments