C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-10-2009, 06:30 PM   #1
"Why use dynamic memory?"
 
Join Date: Aug 2006
Posts: 179
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
OS: Microsoft XP Media Center
IDE: Microsoft Visual Studio 2005 pro edition
Current Porject: Developing a 2D card game...
Hussain Hani is offline   Reply With Quote
Old 07-10-2009, 07:03 PM   #2
Registered User
 
Join Date: Mar 2007
Posts: 333
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   Reply With Quote
Old 07-10-2009, 11:02 PM   #3
Registered User
 
Join Date: Jun 2005
Posts: 1,343
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%.
grumpy is offline   Reply With Quote
Old 07-10-2009, 11:02 PM   #4
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
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
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 07-11-2009, 01:27 AM   #5
&TH of undefined behavior
 
Fordy's Avatar
 
Join Date: Aug 2001
Posts: 5,183
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   Reply With Quote
Old 07-11-2009, 02:12 AM   #6
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
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
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 07-11-2009, 03:04 AM   #7
&TH of undefined behavior
 
Fordy's Avatar
 
Join Date: Aug 2001
Posts: 5,183
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
__________________
"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   Reply With Quote
Old 07-11-2009, 03:13 AM   #8
The larch
 
Join Date: May 2006
Posts: 3,081
Why do I need the instance to call BubbleSort?

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

Quote:
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).
anon is online now   Reply With Quote
Old 07-11-2009, 04:35 AM   #9
Registered User
 
Join Date: Oct 2008
Posts: 452
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   Reply With Quote
Old 07-11-2009, 11:20 AM   #10
Super Moderator
 
Bubba's Avatar
 
Join Date: Aug 2001
Posts: 7,470
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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 05:31 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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