gpt4 book ai didi

c++ - 在 C++ 类方法中使用 *this 完全覆盖自实例化

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:08:55 27 4
gpt4 key购买 nike

下面的代码安全吗? (我已经知道它可以正确编译。)

void Tile::clear()
{
*this = Tile();
}

int main()
{
Tile mytile;

mytile.clear();
}

最佳答案

它可能会起作用。这取决于 Tile& operator = (const Tile&); 是如何实现的。但是,将 *this 赋值给一个新值并没有本质上的错误。

关于c++ - 在 C++ 类方法中使用 *this 完全覆盖自实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3887849/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com