Calling a constructor to re-initialize object

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

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

12条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-04 15:39

    I usually write the following in modern C++ :

    SomeClass a;
    ...
    a = decltype(a)();
    

    It may be not the most effective way, as it effectively constructs another object of the same type of a and assigns it to a, but it works in most cases, you don't have to remember the type of a, and it adapts if the type changes.

提交回复
热议问题