![]() |
| | #1 |
| Robot Join Date: Mar 2009
Posts: 373
| Accessing global constants 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 | |
| | #2 |
| Staff software engineer 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 | |
| | #3 |
| Robot Join Date: Mar 2009
Posts: 373
| |
| Memloop is offline | |
| | #4 |
| C++ Witch 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 | |
| | #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 | |
| | #6 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 12,763
| 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 online now | |
| | #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 | |
| | #8 |
| Staff software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 6,050
| 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 | |
| | #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 | |
| | #10 | |
| Malum in se Join Date: Apr 2007
Posts: 3,188
| Quote:
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 | |
| | #11 |
| Robot Join Date: Mar 2009
Posts: 373
| 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |