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.
The main difference between
class
andnamespace
that aclass
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 aclass
can: there are notemplate
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 aclass
. 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.