3

So it looks like I can't do this for some reason...

class MyClass
{
    static std::string name = "Whatever";
};

I don't get it, for me it would make sense to define something in the declaration, even if is static, but instead, I have to do this... ¿?¿?¿?

class MyClass
{
    static std::string name;
};
std::string MyClass::name = "Whatever";

And by the way... Isn't name private by default? Then why can I change its value from outside the class?

Karlos
  • 197
  • 7

1 Answers1

4

Why I can't define a static field in the declaration?

You can!

#include <string>

class MyClass
{
    static inline std::string name = "Whatever";
    //     ^^^^^^
};

Isn't name private by default? Then why can I change its value from outside the class?

You can't!

You're defining/initialising it, not changing its value.

Asteroids With Wings
  • 17,071
  • 2
  • 21
  • 35