|
-
September 7th, 2008, 09:09 AM
#1
static constant - non integral type
Code:
// header file called .. test.h [1]
static const double dval ( 3.1233 );
// in a source file called main.cpp [2]
class bar {
static const double dbar_val ;
public :
bar () {}
};
double bar::dbar_val = 3.1233;
[1] compiles while [2] does not because only (hope I'm saying this right) static const integral data members can be initialized in a class. I was reading a thread a few days ago where it was said - if memory serves - C++Ox (some version of GCC is already doing this) will recognize [2]. The question: Why is [1] allowed to compile while [2] doesnt? The fact that one is a class and the other is not doesnt seem like a good answer to me.
-
September 7th, 2008, 09:21 AM
#2
Re: static constant - non integral type
The problem here is simply that dbar_val is declared const but defined non-const. Change that line to:
Code:
const double bar::dbar_val = 3.1233;
Note you could also do:
Code:
class bar {
static const double dbar_val = 3.1233;
public :
bar () {}
};
const double bar::dbar_val;
I should add that static class member definitions should always appear in a cpp (or equivalent) file, never in a header unless it is a template class.
- Alon
-
September 7th, 2008, 09:38 AM
#3
Re: static constant - non integral type
Thanks Hermit. That was a bonehead move on my part.
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
|