c++ template singleton static pointer initialization in header file

后端 未结 5 1229
有刺的猬
有刺的猬 2020-12-07 03:13

What is wrong with this implementation in header file?

template 
class Singleton
{
public:
    static T* getInstance() 
    {
        if (m         


        
5条回答
  •  鱼传尺愫
    2020-12-07 04:02

    Pointers are being initialized to NULL by standard, why I need to explicitly initialize?

    You don't need.

    template 
    T* Singleton::m_instance;
    

    This will suffice as static & global variables are zero initialized by default

    Variables with static storage duration (3.7.1) or thread storage duration (3.7.2) shall be zero-initialized (8.5) before any other initialization takes place.

提交回复
热议问题