如何从析构函数中调用const成员函数

时间:2021-05-06 19:52:10

Is there any possible way to invoke const member function from destructor, when const object is destroyed?

当const对象被销毁时,是否有任何可能的方法从析构函数调用const成员函数?

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?

有人可以解释一下为什么cmt被销毁时没有调用const版本的show?

3 个解决方案

#1


18  

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:

在const实例上调用非const重载的原因是因为在销毁期间不考虑当前实例上的cv限定符。 [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.

析构函数用于销毁其类类型的对象。不得采用析构函数的地址。可以为const,volatile或const volatile对象调用析构函数。 const和volatile语义(7.1.6.1)不适用于销毁下的对象。当最派生对象(1.8)的析构函数启动时,它们将停止生效。

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

你可以使用一个简单的绑定* this来引用const来获得你需要的行为:

~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:

或者也许您可以使用存储引用的包装器并在其自己的析构函数中调用show():

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);
};

#2


3  

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.

析构函数应该对其成员进行清理。这在某种意义上说,析构函数应该将当前对象的内容整理为清理。因此,析构函数必须是非常量的。并且,非const mnember函数只能调用所有非const成员函数。这解释了。

#3


2  

I like this question.

我喜欢这个问题。

Destructors can't be const. They behave like any non-const method. A non-const method calls non-const methods.

析构函数不能是const。它们的行为与任何非const方法相同。非const方法调用非const方法。

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.

但是,有很好的理由在析构函数中调用const方法。 (例如记录)。具有非const和const版本,从非const方法调用,非const被调用。

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

要调用const,可以使用static_cast。但是......你无法确定何时施放。 (换句话说:你不知道自己是不是自己)。

#1


18  

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:

在const实例上调用非const重载的原因是因为在销毁期间不考虑当前实例上的cv限定符。 [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.

析构函数用于销毁其类类型的对象。不得采用析构函数的地址。可以为const,volatile或const volatile对象调用析构函数。 const和volatile语义(7.1.6.1)不适用于销毁下的对象。当最派生对象(1.8)的析构函数启动时,它们将停止生效。

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

你可以使用一个简单的绑定* this来引用const来获得你需要的行为:

~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:

或者也许您可以使用存储引用的包装器并在其自己的析构函数中调用show():

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);
};

#2


3  

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.

析构函数应该对其成员进行清理。这在某种意义上说,析构函数应该将当前对象的内容整理为清理。因此,析构函数必须是非常量的。并且,非const mnember函数只能调用所有非const成员函数。这解释了。

#3


2  

I like this question.

我喜欢这个问题。

Destructors can't be const. They behave like any non-const method. A non-const method calls non-const methods.

析构函数不能是const。它们的行为与任何非const方法相同。非const方法调用非const方法。

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.

但是,有很好的理由在析构函数中调用const方法。 (例如记录)。具有非const和const版本,从非const方法调用,非const被调用。

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

要调用const,可以使用static_cast。但是......你无法确定何时施放。 (换句话说:你不知道自己是不是自己)。