Jump to content
  • Advertisement
Sign in to follow this  
CuppoJava

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
as sneftel said you just do it like this

// header file
class foo
{
public:
static const double bar;
static const float baz;
}

// in source file
const double foo::bar = 12.5;
const double foo::baz = 100.134;

Share this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!