# [C++] Static class methods vs namespace

This topic is 3274 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey all. Consider the following: I want to implement some string utilities like trim, convert from std::string to std::wstring and the opposite, split, to lower/upper case and etc. The logic tell that they all should be wrapper inside something called StringUtils. However there are 2 ways to realize StringUtils: 1. Using a namespace: namespace StringUtils{ void trim(const std::string& _text, ...); } 2. Using static methods of class StringUtils: class StringUtils{ static void trim(const std::string& _text, ...); }; How to determine when I should use the first solution and when the second? What are the pros/cons of each of them? Thanks a lot.  If it matters the code is written for DLL, so one favor for second method is that it easier to export class than each particular function. The other question I should ask myself "do I really need to export it or its for internal use?" [/edit]

##### Share on other sites
Basically, a static member function is a global function which can be attributed with access modifiers (public, protected, private) and which can access static members of a class. If you need private, shared data between static member functions, you could also make a "real" global function a friend of a class.

On the client side, each time the user wants to call a static member function, he has to write ClassX::FunctionY(), where with namespaces, he can write using directives.

So, personally, I prefer real global, non-member functions, and also keep my classes as clean as possible.

##### Share on other sites
General rule of thumb: make it a namespace-level function if you can, a member function if you have to.

##### Share on other sites
Putting it in a class doesn't make sense, put it in a namespace.

Also, those facilities are already provided by Boost.StringAlgo

##### Share on other sites
Also by using name spaces you can easily add members to the name space without affecting other code,whereas using static member functions you would have to modify the original class which in some cases might be impossible or prohibited.
Example:

namespace std{	void PrintHello()	{		cout << "Hello World";	}}int main(){std::PrintHello();return 0;}

This would be impossible if std was a class instead of a namespace.

##### Share on other sites
As mentioned previously, the Boost String Algorithms library covers most of the functionality you mentioned. You can find a summary of what the library includes here.

##### Share on other sites
as for boost not interested but thanks.

##### Share on other sites
If the "class" doesn't need state (i.e., it contains only static member functions) I tend to prefer namespaces. I can't see a good reason for introducing another class (and it would be misleading too) when it doesn't need any state and only provides publicly accessible static functions.

##### Share on other sites
Quote:
 Original post by DigitalBeingAlso by using name spaces you can easily add members to the name space without affecting other code,whereas using static member functions you would have to modify the original class which in some cases might be impossible or prohibited.Example:*** Source Snippet Removed ***This would be impossible if std was a class instead of a namespace.

This is a somewhat odd example as adding members to namespace std is not recommended. Why, out of all the millions of possible namespaces, did you have to choose std? :P

##### Share on other sites
Quote:
Original post by Red Ant
Quote:
 Original post by DigitalBeingAlso by using name spaces you can easily add members to the name space without affecting other code,whereas using static member functions you would have to modify the original class which in some cases might be impossible or prohibited.Example:*** Source Snippet Removed ***This would be impossible if std was a class instead of a namespace.

This is a somewhat odd example as adding members to namespace std is not recommended. Why, out of all the millions of possible namespaces, did you have to choose std? :P

Generally yes. But sometimes it could be useful, e.g. if you want to handcode a specialisation of some container, e.g. when you recognize that your compiler produces crap for std::vector<your_type>, where something non-crappy would be possible. Ofc, that's a corner case, but in boost there are parts where you are encouraged to inject entities into its namespace (is_pod).

• 21
• 10
• 19
• 14
• 20