我一直想知道,两次定义琐碎的方法是好还是坏的做法,这取决于
项目是否处于调试/发布状态。这是为了内联它们。例如,Foo.h:
class Foo
{
public:
...
const bool& IsBoolean() const;
private:
bool _boolean;
};
#ifndef _DEBUG
/** We're in release, so let's advice compiler to inline this...
*
*
*/
inline const bool& Foo::IsBoolean() const
{
return _boolean;
}
#endif
现在,在 Foo.cpp 中:
#include "Foo.h"
...
#ifdef _DEBUG
/** We're debugging this, no need for inlining...
*
*
*/
const bool& Foo::IsBoolean() const
{
return _boolean;
}
#endif
这完全没用吗?例如,由于编译器(MSVC)本身内联/优化方法的能力?
尽管如此,这是我多年来一直在使用的东西。请纠正我,如果我在这里完全错了......