Sign in to follow this  
skwee

[C++] Static class methods vs namespace

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. [edit] 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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by DigitalBeing
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:

*** 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 this post


Link to post
Share on other sites
Quote:
Original post by Red Ant
Quote:
Original post by DigitalBeing
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:

*** 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).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this