Calling a constructor to re-initialize object

前端 未结 12 1802
执笔经年
执笔经年 2020-12-04 15:01

is it possible to re-initialize an object of a class using its constructor?

12条回答
  •  难免孤独
    2020-12-04 15:42

    Instead of destructing and reinitializing as suggested by some of the answers above, it's better to do an assignment like below. The code below is exception safe.

        T& reinitialize(int x, int y)
        {
            T other(x, y);
            Swap(other); // this can't throw.
            return *this;
        }
    

提交回复
热议问题