C ++ OOP:哪些函数放入类? [英] C++ OOP: Which functions to put into the class?

查看:117
本文介绍了C ++ OOP:哪些函数放入类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个类a:

class a
{
public:
  void load_data( );
private:
  void check_data( );
  void work_data( );
  void analyze_data( );
}

这些功能都与课程或其中一个成员一起做。

Those functions all do something with the class or one of its members.

但是这个功能:

bool validate_something( myType myData )
{
     if ( myData.blah > 0 && myData.blah < 100 )
     {
        return true;
     }
     return false;
}




  • 与课程相关,只会被它调用,所以它不需要其他任何地方

    • Is related to the class and will only be called by it, so it won't be needed anywhere else

      不对类或其成员做任何事情 - 只是一个小的实用程序功能

      Doesn't do anything with the class or its members - just a small "utility" function

      哪里可以放置 validate_something

      推荐答案

      使其成为一个私人的静态类成员。我以前倾向于使这些非成员成员,并将它们放在实现文件中的无名命名空间中,但似乎他们几乎总是最终需要成为类成员(或需要在其他地方移动 - 也许是为了验证图书馆,在你的例子中),因为代码的变化,所以现在我几乎总是使他们成为静态成员。

      Make it a private static class member. I used to tend to make these non-class members and put them in an nameless namespace in the implementation file, but it seems that they almost always do end up needing to be class members (or need to be moved elsewhere - perhaps to a validation library, in your example), as the code changes, so now I almost always make them static members.

      请注意使用几乎作为限定词答案: - )

      Please notice the use of "almost" as a qualifier throughout this answer :-)

      这篇关于C ++ OOP:哪些函数放入类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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