C Board  

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

Reply
 
LinkBack Thread Tools Display Modes
Old 11-30-2009, 09:00 PM   #1
Robot
 
Join Date: Mar 2009
Posts: 373
Accessing global constants

Do you guys think it's a good idea to always use :: before global constants, even if it's not necessary? I like it because it makes it clear that you are using a global variable, and it prevents you from accidentally using a local variable instead by mistake. I've seen some people prefix global variables with g_, but then you might as well use :: in my opinion.

Then again, the same thing could be said for always using this-> to access member data, which a lot of people probably find to be quite ugly.
Memloop is offline   Reply With Quote
Old 11-30-2009, 09:38 PM   #2
Staff software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 6,050
That sounds like a terrible idea. Why should you deliberate ignore the benefits of namespaces? Namespaces were introduced precisely BECAUSE of name conflicts between globally visible symbols. Placing all global symbols in :: renders namespaces useless.

If you have a hard time remembering that some variable is global, you could A) improve your memory, B) modify the name with a prefix or suffix, or C) eliminate the need for the global variable.
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 11-30-2009, 10:48 PM   #3
Robot
 
Join Date: Mar 2009
Posts: 373
Quote:
Originally Posted by brewbuck View Post
Placing all global symbols in :: renders namespaces useless.
How is that? If you have a variable x in A then use A::x instead.
Memloop is offline   Reply With Quote
Old 11-30-2009, 11:07 PM   #4
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 12,763
I would not say that it renders namespaces useless, but rather, it fails to make good use of namespaces.
__________________
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 online now   Reply With Quote
Old 11-30-2009, 11:09 PM   #5
Robot
 
Join Date: Mar 2009
Posts: 373
I'm not writing code that will used by anyone else, so I don't care about polluting the global namespace.

How exactly does it fail to make good use of namespaces then?
Memloop is offline   Reply With Quote
Old 11-30-2009, 11:17 PM   #6
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 12,763
Quote:
Originally Posted by Memloop
I'm not writing code that will used by anyone else, so I don't care about polluting the global namespace.

How exactly does it fail to make good use of namespaces then?
You are dumping names in the global namespace. If you are fine with that, so be it, it is your problem when you eventually realise your lack of foresight.
__________________
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 online now   Reply With Quote
Old 11-30-2009, 11:24 PM   #7
Registered User
 
Join Date: Apr 2006
Posts: 1,337
Using globals should be pretty rare, so if you find yourself using them enough to feel you need some sort of convention for them, then you are probably over using them.
__________________
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
King Mir is offline   Reply With Quote
Old 11-30-2009, 11:38 PM   #8
Staff software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 6,050
Quote:
Originally Posted by Memloop View Post
I'm not writing code that will used by anyone else, so I don't care about polluting the global namespace.

How exactly does it fail to make good use of namespaces then?
You perform the way you practice. Practice like nobody will ever find value in your code, and your code will always be valueless.
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 11-30-2009, 11:41 PM   #9
Robot
 
Join Date: Mar 2009
Posts: 373
The only reason I could see for using namespaces in this particular case is if I'm linking against some poorly written library that is polluting the global namespace with commonly used names.
Memloop is offline   Reply With Quote
Old 11-30-2009, 11:52 PM   #10
Malum in se
 
abachler's Avatar
 
Join Date: Apr 2007
Posts: 3,188
Quote:
Originally Posted by Memloop View Post
Do you guys think it's a good idea to always use :: before global constants, even if it's not necessary? I like it because it makes it clear that you are using a global variable, and it prevents you from accidentally using a local variable instead by mistake. I've seen some people prefix global variables with g_, but then you might as well use :: in my opinion.

Then again, the same thing could be said for always using this-> to access member data, which a lot of people probably find to be quite ugly.
:: is ugly and bad form. I would fire anyone that used it, well, I'd give them a talking to first, and if they kept it up I'd fire them.

I try to minimize my use of globals, if I need a lot of them, I use a global struct to contain them all. Then I only have to extern a single object to have access to all my global variables.
__________________
Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.
abachler is offline   Reply With Quote
Old 12-01-2009, 12:32 AM   #11
Robot
 
Join Date: Mar 2009
Posts: 373
Quote:
Originally Posted by abachler View Post
I use a global struct to contain them all.
Is that global struct in a namespace? Otherwise, I don't see why accessing it with :: is bad form. It's a clear indication that the struct is indeed global. And if you're using it in a namespace, couldn't you just nest another namespace called "globals" or something within the main namespace instead of using a struct? The only difference would be using globals::constant instead of globals.constant.

As far as I can tell there's no practical difference between prefixing your global variables with g_ and using ::. If there is, please enlighten me.
Memloop is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Whats the best way to control a global class? parad0x13 C++ Programming 3 11-12-2009 05:17 PM
TCL Scripting johndejesus Tech Board 5 08-07-2009 03:35 AM
basic question about global variables radeberger C++ Programming 0 04-06-2009 12:54 AM
Initalizing global variables to constants maxhavoc C Programming 6 06-21-2006 11:25 AM


All times are GMT -6. The time now is 02:36 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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