In C , initializing a static data member of type const std::string directly within the class definition is not permitted. Instead, there are two options to define such data members:
Use an inline variable, which defines and initializes the static member within the class definition:
class A { private: inline static const string RECTANGLE = "rectangle"; };
Define the static member outside the class definition and provide the initializer in a separate implementation file:
Header File
class A { private: static const string RECTANGLE; };
Implementation File
const string A::RECTANGLE = "rectangle";
The syntax of initializing static data members within the class definition is only supported for integral and enum types. For non-integral types like const std::string, this approach is not valid.
The above is the detailed content of How to Initialize Static `const std::string` Data Members in C ?. For more information, please follow other related articles on the PHP Chinese website!