Jump to content
  • Advertisement
Sign in to follow this  
Justindano

Why can't static const float members be initialized in a class?

This topic is 2472 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

Well i was just creating my particle engine in OpenGL and i thought of using classes instead of Structs, and i eventually ran into this static const float..
I never knew they could not be initialized in Class,

For ex,


class cType
{
private:
static const int Num = 10; // Ok.
static const float color = ... // Error
};


Any reasons why?
Thanks!

Share this post


Link to post
Share on other sites
Advertisement
In C++ there is no meaningful difference between class and struct.

Any reasons why?[/quote]

Standard says so. There's likely obscure technical reasons for it, but there is no conspiracy of universe why it could not be done.

Share this post


Link to post
Share on other sites
If you don't want to separate the definition and the declaration, you can go with the function approach.
[source]
class cType
{
private:
static const int Num = 10; // Ok.
static float color() {return ... ;}
};
[/source]

Share this post


Link to post
Share on other sites
As Antheus said, it's because the standard says so. It simply allows for integer types only to be initialized like that, so floating point types have to be defined the normal way with separate definition in a single translation unit. At least pre-C++11...

Share this post


Link to post
Share on other sites
No, it's because float isn't a integral or enumeration type. Under C++03 those were the only kinds of static data members you could initialize in the body of a class or struct.

Share this post


Link to post
Share on other sites
And another thing, Is long-float termed as double, because i got an error message when i used long float in class initializer


static const long float num = 10.0;
Error: a member of type "const double" cannot have an in-class initializer; //Wth?

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!