• Static members vs Namespace
    17 replies, posted
Which is the prefered way and why? [cpp] class Tools { public: static void DoSomething(); }; [/cpp] vs [cpp] namespace Tools { void DoSomething(); } [/cpp]
Is there any difference except of the way to write it?
Inside a class you can use encapsulation.
[QUOTE=DrLuke;22005269]Is there any difference except of the way to write it?[/QUOTE] That's basically my question [QUOTE=animorten;22005394]Inside a class you can use encapsulation.[/QUOTE] Ah yes, didn't think of that :v:
I would prefer a namespace as long as you don't use encapsulation.
They both do different things. Consider two files with the same namespace and class [cpp] // FileA.h class MyFakeNamespace { static void DoSomething(); } namespace MyNamespace { void doSomething(); } // FileB.h class MyFakeNamespace // Error! Multiple Definitions { static void doSomethingElse(); } namespace MyNamespace //Fine { void doSomethingElse(); } [/cpp]
Use namespaces for storing any kind of code somewhere else than in the main-namespace and use classes for interfaces and object-blue-prints. So for your example: Namespace, cause doSomething is static.
Can you use functors in a namespace? Yeah, that's what I thought. :smugdog:
Yes, you can. In C++ at least, if this is the language we are talking about.
[QUOTE=ZeekyHBomb;22087359]In C++ at least, if this is the language we are talking about.[/QUOTE] Yes, it is.
[QUOTE=ZeekyHBomb;22087359]Yes, you can. In C++ at least, if this is the language we are talking about.[/QUOTE] Yeah, and Grizzly Adams had a beard.
I'm not sure what that statement means, but if you want to imply that I am wrong, then try to compile this: [cpp]namespace Ugh{ typedef int *func(bool); func hurr; }[/cpp] and see that it actually works.
[QUOTE=xAustechx;22088729]Yeah, and Grizzly Adams had a beard.[/QUOTE] [img]http://i47.tinypic.com/2laq9p4.png[/img] Grizzly Adams DID have a beard.
If the class makes sense as a class, e.g. you can create instances of it and do things with them, and the static function naturally relates to the class, put it in the class. If the class would just be used as a container for static functions, so there's no point ever making an instance of it, it should be a namespace instead.
[QUOTE=Wyzard;22089194]If the class makes sense as a class, e.g. you can create instances of it and do things with them, and the static function naturally relates to the class, put it in the class. If the class would just be used as a container for static functions, so there's no point ever making an instance of it, it should be a namespace instead.[/QUOTE] I vote this. If it needs encapsulation, give it encapsulation.
[cpp] namespace String { class To { public: static int Int( std::string value ); static bool Bool( std::string value ); // Etc... }; }; [/cpp]
templates use them!
[QUOTE=NorthernGate;22097594][cpp] namespace String { class To { public: static int Int( std::string value ); static bool Bool( std::string value ); // Etc... }; }; [/cpp][/QUOTE] [cpp] namespace String { namespace To { int Int(std::string value); bool Bool(std::string value); // Etc... } } [/cpp] (Although that's a pretty poor example to begin with, such an awful naming scheme and not very template-friendly)
Sorry, you need to Log In to post a reply to this thread.