Is a destructor considered a const function?

后端 未结 2 985
误落风尘
误落风尘 2020-12-09 16:02

Consider this

class Foo
{
public:
    Foo(){}
    ~Foo(){}
    void NonConstBar() {}
    void ConstBar() const {}
};

int main()
{
    const Foo* pFoo = new          


        
相关标签:
2条回答
  • 2020-12-09 16:41

    You can delete objects thorough constant pointers. In C++11, you can an also erase container elements through const-iterators. So yes, in a sense the destructor is always "constant".

    Once the destructor is invoked, the object has ceased to exist. I suppose the question of whether a non-existing object is mutable or not is moot.

    0 讨论(0)
  • 2020-12-09 17:01

    The lifetime of an object ends (for the owner/enclosing scope) as soon as the destructor is invoked, not when the destructor returns.

    Therefore I don't see any problem deleting constants. It's already gone for you when you call delete.

    Otherwise deleting constant objects would require a const_cast.

    0 讨论(0)
提交回复
热议问题