# What! no static double constants in classes?

This topic is 4623 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I just reached the classes chapter in my C++ book, and I'm shocked that I can't declare static double constants in a class. In java, i used the following everywhere...
class A
{
final static double FACTOR = 12.35;
final static int INT_FACTOR = (int)(FACTOR*100);
final static int[] array = new int[INT_FACTOR];
}

How can I make an equivalent thing in C++?
class A
{
static const double FACTOR = 12.35; //not allowed
static const int INT_FACTOR = 1235; //but this is!?
static const int array[INT_FACTOR];
}

Thanks a lot for your help. Honestly, I have to say that I don't like C++ very much, it seems quite inconsistent at times... Alas, the power of pointers comes at a heavy price. -Cuppo

##### Share on other sites
You can have them; you just need to initialize them where they're declared (in the source file), not where they're defined (in the header file).

##### Share on other sites
Only integers and equivalent things (enumerations, etc.) can be initialized in the actual class.

##### Share on other sites
as sneftel said you just do it like this
// header fileclass foo{  public:    static const double bar;    static const float baz;}// in source fileconst double foo::bar = 12.5;const double foo::baz = 100.134;

##### Share on other sites
yea,
but then I can't access the INT_FACTOR variable in order to size my array. And because the INT_FACTOR variable depends on the constant double FACTOR there's no way to get that either.

##### Share on other sites
Quote:
 Original post by SneftelYou can have them; you just need to initialize them where they're declared (in the source file), not where they're defined (in the header file).

Isn't that backwards? The header contains the declaration; the source contains the definition.

##### Share on other sites
Quote:
 Original post by CuppoJavayea,but then I can't access the INT_FACTOR variable in order to size my array. And because the INT_FACTOR variable depends on the constant double FACTOR there's no way to get that either.
Ouch.. Then you're pretty much left with either defines or fixed point integers/enums.
At least I can't think of any other workaround.

##### Share on other sites
The only workaround that I can think of is to declare the constant double FACTOR globally ... or in a namespace. But then its just weird. If all my other variables are under class A, and then my FACTOR variable is found under a separate namespace ... pretty awkward..

##### Share on other sites
If FACTOR is a constant, then INT_FACTOR is a constant, then there's no problem. Just remove the math, and everything is OK.

CM

##### Share on other sites
Quote:
 Original post by CuppoJavaThe only workaround that I can think of is to declare the constant double FACTOR globally ... or in a namespace. But then its just weird. If all my other variables are under class A, and then my FACTOR variable is found under a separate namespace ... pretty awkward..
That doesn't help either, there's no way you'll be able to make an array size out of it (at least I don't think you can..).

Quote:
 Original post by Conner McCloudIf FACTOR is a constant, then INT_FACTOR is a constant, then there's no problem. Just remove the math, and everything is OK.CM
But it's a double so it isn't really a true compile time constant.

• 16
• 11
• 9
• 49
• 12
• ### Forum Statistics

• Total Topics
631392
• Total Posts
2999737
×