Does destroying and recreating an object make all pointers to this object invalid?

后端 未结 6 1626
我寻月下人不归
我寻月下人不归 2021-01-11 23:46

This is a follow-up to this question. Suppose I have this code:

class Class {
    public virtual method()
    {
        this->~Class();
        new( this          


        
6条回答
  •  爱一瞬间的悲伤
    2021-01-12 00:13

    Strictly, this is fine. However, without extreme care, it will become a hideous piece of UB. For example, any derived classes calling this method won't get the right type re-constructed- or what happens if Class() throws an exception. Furthermore, this doesn't really accomplish anything.

    It's not strictly UB, but is a giant pile of crap and fail and should be burned on sight.

提交回复
热议问题