What! no static double constants in classes?

Started by
18 comments, last by cignox1 18 years, 6 months ago
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
Advertisement
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).
Only integers and equivalent things (enumerations, etc.) can be initialized in the actual class.
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;
moe.ron
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.
Quote:Original post by Sneftel
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).

Isn't that backwards? The header contains the declaration; the source contains the definition.
Quote:Original post by CuppoJava
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.
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.
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..
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
Quote:Original post by CuppoJava
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..
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 McCloud
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
But it's a double so it isn't really a true compile time constant.

This topic is closed to new replies.

Advertisement