virtual functions

R

rajesh kalra

do the virtual mechanism works when virtual functions are called in
constructors?
 
I

Ian Collins

rajesh said:
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.
 
J

Juha Nieminen

Ian said:
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).
 
B

Barry

Juha said:
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
 
J

James Kanze

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

ManicQin

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top