在类中派生信息的成员函数 [英] Member functions for derived information in a class

查看:161
本文介绍了在类中派生信息的成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在设计类的接口时,我通常会以两种方式被抓住,无论我是否提供可以通过使用其他成员函数的组合来计算/导出的成员函数。例如:

  class DocContainer 
{
public:
Doc * getDoc(int index )const;
bool isDocSelected(Doc *)const;
int getDocCount()const;

//这个方法应该在这里吗?
//此方法返回禁用者中的所选文档(在selectedDocs_out中)
void getSelectedDocs(std :: vector< Doc *>& selectedDocs_out)const;
};

我应该提供这个作为一个类成员函数还是一个可以定义这个方法的命名空间?哪一个是首选?

解决方案

一般来说,你应该更喜欢自由功能。从OOP的角度来考虑它。



如果该函数不需要访问任何私有成员,那么为什么要给予访问他们?这对封装是不好的。这意味着更多的代码可能会在类的内部修改时可能会失败。



它还限制了可能的代码重用次数。



如果您将函数写成如下形式:

  template< typename T> 
bool getSelectedDocs(T& container,std :: vector< Doc *>&);

然后,getSelectedDocs的相同实现将适用于任何任何类,所需的功能,不仅仅是你的DocContainer。



当然,如果你不喜欢模板,可以使用一个界面,然后它仍然适用于任何类实现了这个接口。



另一方面,如果它是一个成员函数,那么它只适用于这个特定的类(可能是派生类) / p>

C ++标准库遵循相同的方法。例如,考虑 std :: find ,这是由于这个精确的原因而成为一个免费的功能。它不需要知道它正在搜索的类的内部。它只需要满足其要求的一些实现。这意味着相同的 find()实现可以在任何容器,标准库或其他地方工作。



Scott Meyers认为同样的事情



如果你不喜欢这个主要的命名空间,你可以把它放在一个单独的命名空间中,这个特殊的类的功能。


While designing an interface for a class I normally get caught in two minds whether should I provide member functions which can be calculated / derived by using combinations of other member functions. For example:

class DocContainer
{
 public:
   Doc* getDoc(int index) const;
   bool isDocSelected(Doc*) const;
   int getDocCount() const;

  //Should this method be here???
  //This method returns the selected documents in the contrainer (in selectedDocs_out)
  void getSelectedDocs(std::vector<Doc*>& selectedDocs_out) const;
};

Should I provide this as a class member function or probably a namespace where I can define this method? Which one is preferred?

解决方案

In general, you should probably prefer free functions. Think about it from an OOP perspective.

If the function does not need access to any private members, then why should it be given access to them? That's not good for encapsulation. It means more code that may potentially fail when the internals of the class is modified.

It also limits the possible amount of code reuse.

If you wrote the function as something like this:

template <typename T>
bool getSelectedDocs(T& container, std::vector<Doc*>&);

Then the same implementation of getSelectedDocs will work for any class that exposes the required functions, not just your DocContainer.

Of course, if you don't like templates, an interface could be used, and then it'd still work for any class that implemented this interface.

On the other hand, if it is a member function, then it'll only work for this particular class (and possibly derived classes).

The C++ standard library follows the same approach. Consider std::find, for example, which is made a free function for this precise reason. It doesn't need to know the internals of the class it's searching in. It just needs some implementation that fulfills its requirements. Which means that the same find() implementation can work on any container, in the standard library or elsewhere.

Scott Meyers argues for the same thing.

If you don't like it cluttering up your main namespace, you can of course put it into a separate namespace with functionality for this particular class.

这篇关于在类中派生信息的成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆