Calling the vitual functions in the base class destructor

  • Thread starter doublemaster007
  • Start date
D

doublemaster007

1) Can we call pure virtual functions from base class destructor?
2) Can we call virtual functions from base class destructor? which
function will be invoked?

What i think is either of them ends with crashing. because when
destructing the base class destructor, derived subobject is already
destroyed! But i have read some where that (1) ends with crash, second
one is not.
 
B

Bo Persson

1) Can we call pure virtual functions from base class destructor?
2) Can we call virtual functions from base class destructor? which
function will be invoked?

What i think is either of them ends with crashing. because when
destructing the base class destructor, derived subobject is already
destroyed! But i have read some where that (1) ends with crash,
second one is not.

When you are in a constructor or a destructor, the object is always of
exactly the type of the class containing these special members. Any
derived parts have not been constructed yet, or already been
destroyed.

Calling a virtual function works just as it would for an object of the
exact class. The same for pure virtual functions. Whether that
"crashes" or not, is undefined.



Bo Persson
 
S

SG

1) Can we call pure virtual functions from base class destructor?
No.

2) Can we call virtual functions from base class destructor? which
function will be invoked?

Destructors are called in reverse order of constructors. Constructors
are invoked from base class to derived class. After the constructor is
finished, the object IS an instance of the class. After the destructor
is finished the object IS NOT an instance of the class ANYMORE.

If you call a virtual function in a destructor X::~X it will invoke
the function of class X if X overrides the virtual function or the
function of some other base class.

Cheers!
SG
 
T

terminator

1) Can we call pure virtual functions from base class destructor?
2) Can we call virtual functions from base class destructor? which
function will be invoked?

What i think is either of them ends with crashing. because when
destructing the base class destructor, derived subobject is already
destroyed! But i have read some where that (1) ends with crash, second
one is not.

It seems that case 1 is likely to happen.If the compiler does not
catch this error (first senario),a runtime or rational error is
expected .I call the later two senarios a **crash**.
I do not not what standard says about this case.

regards,
FM.
 
T

terminator

1) Can we call pure virtual functions from base class destructor?
2) Can we call virtual functions from base class destructor? which
function will be invoked?

What i think is either of them ends with crashing. because when
destructing the base class destructor, derived subobject is already
destroyed! But i have read some where that (1) ends with crash, second
one is not.

PS:it is also possible that a null function exeption is automatically
neglected .

regards,
FM.
 
D

doublemaster007

PS:it is also possible that a null function exeption is automatically
neglected .

regards,
FM.

Hmm..if we call virtual functions from base class functions it would
call dervied class implementation

but in i call virtual functions from base class destructor it would
call base class functions? Why the compilers dont behave as normaly
user predicts? I think most of the people would think otherwise in
this case i guess..
 
J

James Kanze

It seems that case 1 is likely to happen.If the compiler does
not catch this error (first senario),a runtime or rational
error is expected .I call the later two senarios a **crash**.
I do not not what standard says about this case.

If the call is through an unqualified name (i.e. just f(), and
not C::f()), and the function is virtual, dynamic lookup is
used. If dynamic lookup resolves to a pure virtual function,
the behavior is undefined (although most compilers will cause
the program to crash).

In the destructor, the dynamic type of the object is the type of
the destructor. So if the function is declared pure virtual in
that class, calling it with an unqualified name is undefined
behavior. You can still call it with a qualified name, however.
(In that case, of coures, you have to provide an
implementation.)
 
J

James Kanze

PS:it is also possible that a null function exeption is
automatically neglected .

There is no such thing as a null function exception. (For that
matter, there is no such thing as a null function.)

It's undefined behavior, so anything can happen. From a QoI
point of view, I would expect the program to crash (assertion
failure, segment violation or such---either the code tries to
dereference a null pointer, or the compiler inserts a pointer to
a function which aborts with an error message).
 
J

James Kanze

Hmm..if we call virtual functions from base class functions it
would call dervied class implementation
but in i call virtual functions from base class destructor it
would call base class functions?

If they exist and are not pure virtual. Once you've reached the
base class destructor, the derived class object doesn't exist
any more, and it makes no sense (and is extremely dangerous) to
call a member function on something that doesn't exist.
Why the compilers dont behave as normaly user predicts? I
think most of the people would think otherwise in this case i
guess..

And why would people think that you would call a function on
something that doesn't exist?
 
T

terminator

There is no such thing as a null function exception.  (For that
matter, there is no such thing as a null function.)

It's undefined behavior, so anything can happen.  From a QoI
point of view, I would expect the program to crash (assertion
failure, segment violation or such---either the code tries to
dereference a null pointer, or the compiler inserts a pointer to
a function which aborts with an error message).

ok.my hands are up.**UB==UB**.

thanks,
FM.
 
D

doublemaster007

If they exist and are not pure virtual.  Once you've reached the
base class destructor, the derived class object doesn't exist
any more, and it makes no sense (and is extremely dangerous) to
call a member function on something that doesn't exist.


And why would people think that you would call a function on
something that doesn't exist?

--
James Kanze (GABI Software)             email:[email protected]
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- Hide quoted text -

- Show quoted text -

but people would never expect that compiler will show some extra
intelligency and call base class functions! similar type of question
found in one of the C++ interview. 99% of them answered calling
virtual functions inside base class destructoe leads to crash...[that
means most of the people think that it derived object is destroyed and
any invocation of virtual function would crash]
 
I

Ian Collins

Please don't quote signatures (eben malformed ones like James'!).
but people would never expect that compiler will show some extra
intelligency and call base class functions! similar type of question
found in one of the C++ interview. 99% of them answered calling
virtual functions inside base class destructoe leads to crash...[that
means most of the people think that it derived object is destroyed and
any invocation of virtual function would crash]

Destroying an object is like pealing an onion; each layer is stripped
away until you reach the base class. When you get there, the object is
an instance of the base.
 

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

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top