Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ OOP: Which functions to put into the class?

Assume I have a class 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.

However this function:

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

Where to put validate_something? Inside or outside the class?

like image 378
oh boy Avatar asked Apr 29 '10 16:04

oh boy


1 Answers

If a function is

  • not required outside of the class, and,
  • doesn't need to access class members (or perhaps makes sense as a free function with members as parameters)

then I tend to make it a free function inside a private namespace in the implementation file. I prefer not to use private functions because they expose implementation detail in the header file (and haven't yet encountered Neil's problems) and I prefer to avoid recompiling for implementation changes.

like image 146
Adam Bowen Avatar answered Sep 29 '22 02:09

Adam Bowen