我在许多旧版代码中看到了以下模式。我不熟悉它,也无法理解为什么它在那里。
在头文件中:
struct ook
{
bool func_called; // Not declared as const!
bool func();
ook();
};
在源文件中:
ook::ook():
func_called( func() )
{} // ← Nothing there whatsoever.
bool ook::func()
{
// … ← Some stuff without a "return" statement of any kind.
return true; // This does mean that func_called is always true.
}
这是有用的东西还是奇怪的东西造成的可怕的复制和粘贴错误?
现在,仅在构造函数中调用
func_called
,而在代码中则不再调用。如Florian Castellane在his answer中指出的那样,如果有的话,则有意义。如果只发生一次,我可以想象这曾经被使用过。但是,它在代码库中发生了数十次,所以我想知道它还有什么其他用途。只是要更清楚一点,这不是我的代码。我只是想了解(并且没有历史记录,注释或单元测试)逻辑是什么。也许没有希望了吗?
最佳答案
假定true == func_called
的其余代码测试,可以用来确保使用构造函数初始化结构。
关于c++ - 奇怪的构造模式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41764658/