-
April 5th, 2010, 05:45 PM
#1
global constants - two alternatives...
Given:
Code:
# include <iostream>
//In a file called ... global_constants.h
class global_constants1
{
public :
static const int dummy = 5;
static const double dprec_value1 ;
};
//global_constants.h or global_constants.cpp
const double global_constants1::dprec_value1 = 4.3 ;
//In a file called ... global_constants.h
namespace global_constants2
{
const int dummy = 5;
double const dprec_value1 = 4.3 ;
}
int main()
{
std::cout << global_constants2::dummy << std::endl;
std::cout << global_constants1::dummy << std::endl;
std::cin.get();
}
The code show reflects two alternatives to global constants definition. I'm able to deduce from the standard that constants have internal linkage. That said, I think you'll get internal linkage in both versions and any decent compiler will eliminate unused symbols in translation unit that includes these files. Simply put there's no advantage of one over the other. True or False?
-
April 5th, 2010, 06:14 PM
#2
Re: global constants - two alternatives...
If you're really concerned about unused symbols and whatnot, then why not use the preprocessor? Simple constants are one area in which #defines don't have any real downside.
-
April 5th, 2010, 07:54 PM
#3
Re: global constants - two alternatives...
Originally Posted by Lindley
Simple constants are one area in which #defines don't have any real downside.
Unless you want to examine their value in the debugger.
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
April 5th, 2010, 08:09 PM
#4
Re: global constants - two alternatives...
Originally Posted by Lindley
If you're really concerned about unused symbols and whatnot, then why not use the preprocessor? Simple constants are one area in which #defines don't have any real downside.
My concern goes beyond 'unused symbols'. I'm trying to make sense of a coding standard that suggests using the 'global_constants1' option.
-
April 5th, 2010, 08:23 PM
#5
Re: global constants - two alternatives...
Originally Posted by VladimirF
Unless you want to examine their value in the debugger.
Possible, I suppose, if they're just aliased to other constants. In that case, another approach would probably be preferable. But for many cases, there's little incentive to directly examine them in the debugger since....well, they don't change from the value you assign them, by definition.
-
April 6th, 2010, 06:31 AM
#6
Re: global constants - two alternatives...
Originally Posted by Lindley
If you're really concerned about unused symbols and whatnot, then why not use the preprocessor? Simple constants are one area in which #defines don't have any real downside.
#define-s ignore namespaces. I try to avoid them as much as possible.
-
April 6th, 2010, 07:52 AM
#7
Re: global constants - two alternatives...
That's a good point, particularly when defining a library rather than an end program.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|