Is there any possible way to invoke const member function from destructor, when const object is destroyed?
Consider:
struct My_type {
~My_type () {
show ();
}
void show () {
cout << "void show ()" << endl;
}
void show () const {
cout << "void show () const" << endl;
}
};
And usage:
My_type mt;
const My_type cmt;
mt.show ();
cmt.show ();
Output:
void show ()
void show () const
void show ()
void show ()
Can someone explain me why const version of show has not been invoked when cmt is destroyed?
I like this question.
Destructors can't be const. They behave like any non-const method. A non-const method calls non-const methods.
But, there are good reasons to call const methods in destructors. (Logging for instance). Having both, a non-const and a const version, called from a non-const method, the non-const is called.
To call the const, it is possible to use static_cast. But... you cannot determine when to cast. (In other words: You don't know if you are const yourself).
Destructor is supposed to do the cleanup of its members/ This, in a sense, destructor is supposed tomodify the contents of the current object whole doing the cleanup. So, destructor has to be non-const. And, a non-const mnember function can call all non-const member function only. This explains.
The reason the non-const overload is being called when on a
const
instance is because cv-qualifiers on the current instance aren't considered during destruction. [class.dtor]/p2:You can use a simply bind
*this
to a reference toconst
to get the behavior you need:Or maybe you can use a wrapper that stores a reference and calls
show()
in its own destructor: