How to initialize static members in the header

前端 未结 8 1138
伪装坚强ぢ
伪装坚强ぢ 2020-12-13 09:02

Given is a class with a static member.

class BaseClass
{
public:
    static std::string bstring;
};

String has obviously to be default-

8条回答
  •  遥遥无期
    2020-12-13 09:11

    If the initializer can be expressed as a literal, it is solved in C++11:

    inline std::string operator"" _s(const char* p, size_t n) {
        return std::string{p, n};
    }
    
    class BaseClass {
        // inline initialization using user-defined literals
        // should allow for multiple definitions
        std::string bstring{"."_s};
    };
    

提交回复
热议问题