Static const string won't get initialized

后端 未结 6 1648
一生所求
一生所求 2021-01-13 21:00

I have some static const strings as private members of my C++ class. I am aware of the declaration in .h and definition (and initialization) in .cpp practice. In the class c

6条回答
  •  无人共我
    2021-01-13 21:47

    I'd use a different approach:

    class Test
    {
    public:
        Test() : m_data( "Data" ) {}
    private:
         const std::string m_data;
    };
    

    I personally prefer this 'style' and it would remove any issues with having initialised data in the constructor at what is likely to be the minor cost of constructing the string for each instance.

提交回复
热议问题