为什么在c ++中的单例类创建过程中静态函数不能引用静态变量? [英] why cannot static function reference static variable during singleton class creation in c++?

查看:179
本文介绍了为什么在c ++中的单例类创建过程中静态函数不能引用静态变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解单例设计模式,并创建了一个最简单的模式:

  #include< iostream> 


class mySingleton {

private:
static mySingleton * ptr;
mySingleton(){}

public:
static mySingleton * getInstance(){
if(!ptr){
ptr = new mySingleton ;
return ptr;
} else return ptr;
}

void msg(){
std :: cout< Hello World !!<< std :: endl;
}

};


int main(){

mySingleton * obj = mySingleton :: getInstance();
mySingleton * obj2 = mySingleton :: getInstance();

return 0;
}

当我尝试编译时,我得到:



架构x86_64的未定义符号:
mySingleton :: ptr,引用自:
mySingleton :: getInstance()in ccm822LI.o
ld:没有为架构x86_64找到符号
collect2:错误:ld返回1退出状态

为什么我不能在静态函数中使用ptr,因为ptr也是一个静态变量?我在这里缺少什么?

解决方案


我缺少这里的东西吗?


是的,有几件事情:


  1. 如前所述,您缺少静态 mySingleton 指针变量的定义。

  2. 您的代码不是线程安全的

    实现它的正确方法是在 getInstance()函数中使用本地静态变量aka。 Scott Meyer的Singleton ):

      static mySingleton * getInstance(){
    static mySingleton theInstance;
    return& theinstance;
    }

    此实现保证线程安全,并且您不需要


  3. 使用指针可能不是您想要的作为返回类型

      static mySingleton& getInstance(){
    // ^
    static mySingleton theInstance;
    return theinstance;
    }



I was trying to understand singleton design pattern and created a simplest one:

#include <iostream>


class mySingleton{

private:
   static mySingleton *ptr;
   mySingleton(){ }    

public:
   static mySingleton* getInstance(){
     if(!ptr){
        ptr = new mySingleton();
        return ptr;
     } else return ptr;
   }

   void msg(){
     std::cout << " Hello World!! " << std::endl;
   }

};


int main(){

mySingleton* obj = mySingleton::getInstance();
mySingleton* obj2 = mySingleton::getInstance();

return 0;
}

When I try to compile I get :

Undefined symbols for architecture x86_64:
"mySingleton::ptr", referenced from:
    mySingleton::getInstance()       in ccm822LI.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

Why can't I use ptr inside a static function, since ptr is also a static variable? Am I missing something here?

解决方案

Am I missing something here?

Yes, several things:

  1. As mentioned you are missing the definition of the static mySingleton pointer variable.
  2. Your code isn't thread safe
    The correct way to implement it is to use a local static variable in the getInstance() function (aka. Scott Meyer's Singleton):

    static mySingleton* getInstance(){
        static mySingleton theInstance;
        return &theinstance;
    }
    

    This implementation is guaranteed to be thread safe, and you don't need to bother with memory allocation.

  3. Using a pointer probably isn't what you want as a return type

    static mySingleton& getInstance(){
                   // ^
        static mySingleton theInstance;
        return theinstance;
    }
    

这篇关于为什么在c ++中的单例类创建过程中静态函数不能引用静态变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆