![]() |
| | #1 |
| "Why use dynamic memory?" Join Date: Aug 2006
Posts: 179
| namespaces or static classes ?? you decide 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
};
Code: const double PI = HussainLib::PI() 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 OS: Microsoft XP Media Center IDE: Microsoft Visual Studio 2005 pro edition Current Porject: Developing a 2D card game... |
| Hussain Hani is offline | |
| | #2 |
| Registered User Join Date: Mar 2007
Posts: 332
| 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.
__________________ home page (new layout) |
| scwizzo is offline | |
| | #3 | |
| Registered User Join Date: Jun 2005
Posts: 1,337
| Quote:
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%. | |
| grumpy is offline | |
| | #4 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,211
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is offline | |
| | #5 |
| &TH of undefined behavior Join Date: Aug 2001
Posts: 5,181
| If you are going to use this trick, then why not look up the singleton pattern.
__________________ "If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping your mouth shut." Albert Einstein (1879 - 1955) Board Rules |
| Fordy is offline | |
| | #6 | |||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,211
| Quote:
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:
Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |||
| laserlight is offline | |
| | #7 |
| &TH of undefined behavior Join Date: Aug 2001
Posts: 5,181
| Maybe, but personally I'd rather stick with the singleton pattern - it achieves pretty much the same thing and has a few added extras
__________________ "If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping your mouth shut." Albert Einstein (1879 - 1955) Board Rules |
| Fordy is offline | |
| | #8 | |
| The larch Join Date: May 2006
Posts: 3,059
| Why do I need the instance to call BubbleSort? Code: MyLib::GetInstance().BubbleSort(data);
__________________ I might be wrong. Quote:
| |
| anon is online now | |
| | #9 |
| Registered User Join Date: Oct 2008
Posts: 450
| 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. |
| EVOEx is offline | |
| | #10 |
| Super Moderator Join Date: Aug 2001
Posts: 7,455
| 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?
__________________ If you aim at everything you will hit something but you won't know what it is. |
| Bubba is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Seg Fault in Compare Function | tytelizgal | C Programming | 1 | 10-25-2008 03:06 PM |
| seg fault at vectornew | tytelizgal | C Programming | 2 | 10-25-2008 01:22 PM |
| need help wiht using classes...namespaces... | hockey97 | C++ Programming | 9 | 09-02-2008 02:22 PM |
| Question about Static variables and functions. | RealityFusion | C++ Programming | 2 | 10-14-2005 02:31 PM |
| Using private class members in static functions | sethjackson | C++ Programming | 2 | 09-23-2005 09:54 AM |