how to properly intialize static const user defined types

how to properly intialize static const user defined types?

1
2
3
4
5
6
class MyClass
{
   public:
   static const sf::Vector2f defaultPos = sf::Vector2f(WINDOW_HEIGHT/GAMEOBJECT_HEIGHT,WINDOW_WIDTH/GAMEOBJECT_WIDTH); //error
    
};
Line 4: You can't initialize a static const with a function call.

If you remove the const, you can initialize it where you initialize any globals:
 
  MyClass::defaultPos = sf::Vector2f (WINDOW_HEIGHT/GAMEOBJECT_HEIGHT, WINDOW_WIDTH/GAMEOBJECT_WIDTH);

Looks like it's not a constant expression, because of the function call:

cppreference wrote:
Constant static members

If a static data member of integral or enumeration type is declared const (and not volatile), it can be initialized with a initializer in which every expression is a constant expression, right inside the class definition:


http://en.cppreference.com/w/cpp/language/static

Topic archived. No new replies allowed.