namespaces or static classes ?? you decide

This is a discussion on namespaces or static classes ?? you decide within the C++ Programming forums, part of the General Programming Boards category; In a book i'm reading about C++ software programming called : Large-Scale C++ Software Design The author does not utilize ...

  1. #1
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186

    namespaces or static classes ?? you decide

    In a book i'm reading about C++ software programming called :
    Large-Scale C++ Software Design

    The author does not utilize namespaces at all specially that he is taking about how to use C++ for extensive, large projects

    instead, he uses static classes like this:
    Code:
    class HussainLib
    {
    	HussainLib(); //no class instantiation meant
    
    	//functions
    public:
    	static void bubble_sort(int* list, int size);
    	static void arithmetic(int x, int y, int arith);
                    //blah blah blah
    
    	//data
    public:
    	static const double PI();
    	static const double e();
                    //blah blah blah
    };
    and of course you would call them like this:
    Code:
    const double PI = HussainLib::PI()
    what do you think? is it more practical than namespaces ?
    I find it a very good way to hide data and functions for programmers working together
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    I think it depends on the needs and uses of the class/namespace. I know of some software APIs/Devkits that have classes within namespaces to differentiate between which one you want. Example: if you have a class that decodes audio formats like mp3, or wave, or ogg, and the class has the same function names/declaration, what then? Namespace the class so the compiler knows which class you want to use.

    IMHO, it's preference.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,169
    Quote Originally Posted by Hussain Hani View Post
    The author does not utilize namespaces at all specially that he is taking about how to use C++ for extensive, large projects

    what do you think? is it more practical than namespaces ?
    I find it a very good way to hide data and functions for programmers working together
    As a matter of fact, there is no real distinction with standard C++. All static and non-static members of a class exist within a namespace with the same name as the class.

    There was a distinction with some older (almost antique) compilers, as early (draft) versions of the C++ standard supported classes but did not specifically support the concept of namespaces.
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    Quote Originally Posted by Hussain Hani
    The author does not utilize namespaces at all specially that he is taking about how to use C++ for extensive, large projects
    That is because C++ was standardised after that book was published, and though I have little knowledge of the state of C++ at the time, my guess from my use of MVSC6 is that the namespace feature in its current form was far from widely adopted at the time of writing of that book.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    If you are going to use this trick, then why not look up the singleton pattern.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    Quote Originally Posted by Hussain Hani
    what do you think? is it more practical than namespaces ?
    Either way seems practical to me. However, consider that you can "re-open" a namespace, but have to change the class definition to add a new static member function. Also, consider that you can use using declarations and using directives with namespaces where appropriate and convenient, but not so for classes, where using declarations are mainly used to make visible names from a base class that would otherwise be hidden.

    There is also the option of using an anonymous namespace in a source file to hold functions that are implementation detail and yet do not need direct access to the internals of the class(es) being implemented.

    Quote Originally Posted by Hussain Hani
    I find it a very good way to hide data and functions for programmers working together
    Eh, but the idea is to group, not hide, as opposed to "normal" usage of classes where the class serves as an abstraction.

    Quote Originally Posted by Fordy
    If you are going to use this trick, then why not look up the singleton pattern.
    I do not think that that would be correct: from what I understand, the idea here is to use a class that cannot be instantiated to provide a namespace for a set of (otherwise) free functions.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Quote Originally Posted by laserlight View Post
    I do not think that that would be correct: from what I understand, the idea here is to use a class that cannot be instantiated to provide a namespace for a set of (otherwise) free functions.
    Maybe, but personally I'd rather stick with the singleton pattern - it achieves pretty much the same thing and has a few added extras

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Why do I need the instance to call BubbleSort?

    Code:
    MyLib::GetInstance().BubbleSort(data);
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    I have seen cases where a class with static functions was more useful. I can't think of the exact situation, but I remember they were two "namespaces" with similar functionality, however which was to be used depended on something (the system, the configuration, whatever). In this case, a class was better because I could instantiate the class and pass it to a function and call the functions of the class inside that function. It was the best design I could think of at that point.

  10. #10
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    I hope I never have to maintain code like that. Too many statics always throws a red flag for me. Seems pointless to me since there are several design patterns out there that do much the same. If you are going to do things that way why use C++ at all?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. need help wiht using classes...namespaces...
    By hockey97 in forum C++ Programming
    Replies: 9
    Last Post: 09-02-2008, 02:22 PM
  4. Question about Static variables and functions.
    By RealityFusion in forum C++ Programming
    Replies: 2
    Last Post: 10-14-2005, 02:31 PM
  5. Using private class members in static functions
    By sethjackson in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2005, 09:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21