1

I originally created a class like so:

class A
{
  public:
   void run(int x);

  private:
   void run_helper1();
   void run_helper2();
   void run_helper3();

   int a_;
   double b_;
   bool c_;
};

Later I realized it really didn't need any state, I just needed the functions. Would it make sense to drop the class and make these free functions in a namespace? If so, I lose the concept of public and private and end up with run_helper1(), run_helper2(), run_helper3() all being public, if I'm not mistaken. That seems like a poor design.

cigien
  • 57,834
  • 11
  • 73
  • 112
jlev
  • 21
  • 2
  • If you were to go the namespace route, there are a couple of things you could do. First, don't put declarations for your private helpers in the header file. They are private implementation details that can appear only in your implementation file. Second, you could put them in an unnamed namespace in that implementation file so that they are not directly accessible from other translation units. – Avi Berger Dec 03 '22 at 00:43
  • You say your class doesn't need state, but your example class has three data members i.e. it has state. That obscures your problem. In any event, it is possible to place only `run()` into a namespace, and (in the source file that defines that function) place the `run_helper()` functions into either an unnamed namespace or make them `static` functions at file scope - either option prevents them being called directly from other source files. – Peter Dec 03 '22 at 00:45
  • If you don't have any state, just make all the functions static. No need to have an implicit `this` argument. Perfectly fine to put them in a class rather than namespace. – doug Dec 03 '22 at 01:13
  • In the olden days, before we had `namespace` in the language, some programmers would use a class with static functions — which would never be instantiated as an object — as a kind of namespace. But the future is now, and we have `namespace` which better conveys the intent. – Eljay Dec 03 '22 at 01:43
  • @Peter Sorry, should have clarified that when I said I realized it didn't need state, I meant that I could remove those three data members. – jlev Dec 03 '22 at 03:08

1 Answers1

2

The main difference between class and namespace that a class is closed (but possibly extensible using inheritance) and holds an invariant; while a namespace is open and can be extended at any point. Invariant might be as simple as having a unique address (and being able to compare instance addresses), but, if I understand you correctly, even that is unnecessary.

If there's really no other use of A than to 'group together' functions, then your intuition might be right and you might want change it to a namespace.

There is, however, an example what a namespace can't do that a class can: there are no template namespaces. Thus, if you ever need to pass the methods together, e.g. as an API (or a versioned API), then you need to keep them as a class. In that case, callee templates over the whole collection of functions and you can have multiple such collections; but it's a rather rare use-case.

Thus, normally you can convert it.

lorro
  • 10,687
  • 23
  • 36