How can I call const member function from destruct

2019-03-17 06:22发布

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?

3条回答
混吃等死
2楼-- · 2019-03-17 06:27

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

查看更多
萌系小妹纸
3楼-- · 2019-03-17 06:31

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.

查看更多
小情绪 Triste *
4楼-- · 2019-03-17 06:32

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:

A destructor is used to destroy objects of its class type. The address of a destructor shall not be taken. A destructor can be invoked for a const, volatile or const volatile object. const and volatile semantics (7.1.6.1) are not applied on an object under destruction. They stop being in effect when the destructor for the most derived object (1.8) starts.

You can use a simply bind *this to a reference to const to get the behavior you need:

~My_type() { 
    My_type const& ref(*this);
    ref.show();
}

Or maybe you can use a wrapper that stores a reference and calls show() in its own destructor:

template<class T>
struct MyTypeWrapper : std::remove_cv_t<T> {
    using Base = std::remove_cv_t<T>;
    using Base::Base;

    T&       get()       { return ref; }
    T const& get() const { return ref; }

    ~MyTypeWrapper() { ref.show(); }
private:
    T& ref = static_cast<T&>(*this);
};
查看更多
登录 后发表回答