简体   繁体   English

C ++类设计“助手功能”

[英]C++ class design “helper functions”

I have created a basic class that creates an adjacency list (graph theory). 我创建了一个创建邻接表(图论)的基本类。 I have coded a depth first search function but it is of poor design and is currently 50 lines long. 我已经对深度优先搜索功能进行了编码,但是它的设计较差,目前长度为50行。 I am trying to reduce the size and improve readability of the function. 我试图减小尺寸并提高功能的可读性。

template <class T>
class adj_list
{
  public:
   void add_node (const T data);
   void add_edge(const T first, const T second);
   void remove_node (const T data); 
   void remove_edge(const T first, const T second);
   void dfs(const T node, const T lookfor);
   void print_list() const;

  private:
   std::map<T, std::set<T>> graph;

};

So I will need to have 2-3 "helper" (not sure what to call these) functions which do specific things in the dfs algorithm. 因此,我将需要有2-3个“帮助程序”(不确定如何称呼它们)函数,这些函数在dfs算法中完成特定的工作。 They will have to read the private graph, but not modify it. 他们将不得不读取私有图,但不能对其进行修改。

Is my best option to just add these new smaller functions as public members? 我最好的选择只是将这些新的较小功能添加为公共成员吗? I don't think that I want a user to be using these functions. 我不希望用户使用这些功能。 What is the best way to go about this? 最好的方法是什么?

Is my best option to just add these new smaller functions as public members? 我最好的选择只是将这些新的较小功能添加为公共成员吗? I don't think that I want a user to be using these functions. 我不希望用户使用这些功能。 What is the best way to go about this? 最好的方法是什么?

Add them as private members. 将他们添加为私人成员。

If those methods you are talking about are going to be called outside the class, they will need to be defined as public , and due the fact you are not going to change the private members, also you can define them as public static . 如果要在类之外调用这些方法,则需要将它们定义为public ,并且由于您不打算更改私有成员,您也可以将它们定义为public static

If the methods are going to be called only from other methods of the class, then you should declare them as private . 如果只从类的其他方法调用这些方法,则应将它们声明为private

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM