Reputation: 21508
Is there any advantage to using private
(probably also static
) functions in a class for utility functions used in my class that do not need access to an instance's data over using global static
functions in my .cpp
file that implements the class?
The first sounds cleaner to me, but the second really makes more sense as these functions do not need to even be mentioned in the .h
file.
Upvotes: 26
Views: 10604
Reputation: 38543
If the private function doesn't modify the class members, it doesn't have any advantage over the global static. Being inside or outside the class makes no difference
Upvotes: 0
Reputation: 13451
I would not put private static functions to the header file if they are not needed. They would just pollute the header file and add more work.
But private static functions may be needed when you have a template method/function in a class and want to use that helper function in it.
Another reason for using private static functions instead of global static functions is that they can access private class members (variables, functions).
Upvotes: 13
Reputation: 24546
Just make them file-static functions. If they don't have anything to do with the class, don't put them there.
Upvotes: 5
Reputation: 69988
If a given function relates to your class
then you are right. You should make them private static
within your class
body.
[Note: If those utility function doesn't relate at all then you can think about enclosing them in a namespace
or another Util class and keep it within the file scope.]
Upvotes: 5