gpt4 book ai didi

c++ - 在析构函数中进行函数调用是一种好习惯吗?

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

假设有一个对象的析构函数:

anObject::~anObject()
{
_functionCalledfromDestructor=1; //this sets the flag = 1
functionCall(); //this function does something different than usual
//on seeing the flag
}

我的问题:

这种在析构函数中编码的风格/方法是一种好的做法吗?

最佳答案

像这样的“隐藏 channel ”总是一个坏主意。函数的行为不应依赖于不可见状态。您可以给函数一个参数,然后在析构函数中传递一个值,在其他地方传递另一个值。如果您愿意,可以为更常见的情况使用默认值。

关于c++ - 在析构函数中进行函数调用是一种好习惯吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11548543/

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