How to initialize private static members in C++?

后端 未结 17 2268
忘掉有多难
忘掉有多难 2020-11-21 07:04

What is the best way to initialize a private, static data member in C++? I tried this in my header file, but it gives me weird linker errors:

class foo
{
           


        
17条回答
  •  孤独总比滥情好
    2020-11-21 07:18

    Since C++17, static members may be defined in the header with the inline keyword.

    http://en.cppreference.com/w/cpp/language/static

    "A static data member may be declared inline. An inline static data member can be defined in the class definition and may specify a default member initializer. It does not need an out-of-class definition:"

    struct X
    {
        inline static int n = 1;
    };
    

提交回复
热议问题