简体   繁体   中英

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. 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. 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 .

If the methods are going to be called only from other methods of the class, then you should declare them as private .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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