C++ where to initialize static const

前端 未结 5 1174
走了就别回头了
走了就别回头了 2020-11-27 11:17

I have a class

class foo {
public:
   foo();
   foo( int );
private:
   static const string s;
};

Where is the best place to initialize the

5条回答
  •  温柔的废话
    2020-11-27 11:24

    Since C++17 the inline specifier also applies to variables. You can now define static member variables in the class definition:

    #include 
    
    class foo {
    public:
       foo();
       foo( int );
    private:
       inline static const std::string s { "foo" };
    };
    

提交回复
热议问题