考虑Bjarne Stroustrup的c ++编程语言的16.3.2辅助函数,假定我们有一个接口,
namespace IO
{
class IImge
{
public:
virtual double getThreshold() = 0;
...
}
}
我们有一个具体的课程
namespace IO
{
class Image :public IImge
{
public:
double getThreshold() override {return 20.0}
}
}
并假设我们要添加这样的辅助函数
double getNormalizedThreshold(IImage* image) {return image->getThreshold() / 100;}
将函数声明放在哪里?在IImage或Image中?
由于您的助手功能不需要知道Image
,因此放置它的逻辑位置是IImage
。
我认为这个问题是基于观点的。我个人对此的看法是,辅助函数应该在名称空间中声明,而不是在任何类内部声明,而应仅使用此类的公共API。它们应按用途和预期用途分组。