C++ 中的单例类

发布于 2024-12-27 14:50:04 字数 801 浏览 1 评论 0原文

我按照以下示例使用了单例类:

单例类

但我得到的错误为 “无法解析的外部符号“

这是我尝试过的代码:

#include<iostream>
using namespace std;

class singleton
{
    int value;
    static singleton *instance;
protected:
    singleton()
    {
        value=0;
    }
public:
    static void initialize()
    {
        if(instance==NULL)
            singleton();
        else
            cout<<"An instance of singleton already exist...";
    }
    static singleton& getInstance()
    { 
        return *instance; 
    }
    int getValue() 
    { 
        return value; 
    }

};

void main()
{
    singleton::initialize();
}

对 Singleton 类进行一点解释就太好了。它使用的场景。优点和缺点。单例的替代品。等等

I have used singleton calss following the example:

singleton class

But i get the error as "Unresolved external symbols"

this is the code i tried out:

#include<iostream>
using namespace std;

class singleton
{
    int value;
    static singleton *instance;
protected:
    singleton()
    {
        value=0;
    }
public:
    static void initialize()
    {
        if(instance==NULL)
            singleton();
        else
            cout<<"An instance of singleton already exist...";
    }
    static singleton& getInstance()
    { 
        return *instance; 
    }
    int getValue() 
    { 
        return value; 
    }

};

void main()
{
    singleton::initialize();
}

A little bit explanation on Singleton classes would be really great. The scenario its used. advantages and drawbacks. Alternatives to Singleton. etc

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酒绊 2025-01-03 14:50:04

首先,我认为:

singleton();

应该是:

instance = new singleton();

按照您的方式,您实际上并没有存储新实例化的对象,因此 instance始终为空。

使用以下方式显式设置静态也是一种很好的形式:(

singleton *singleton::instance = 0;

在类定义之外)。

事实上,最好从基线单例代码开始,然后从那里开始逐步完善。这是标准形式的指针版本:

#include <iostream>

class singleton {
    protected:
        static singleton *instance;
        singleton() { }
    public:
        static singleton *getInstance() {
            if (instance == 0)
                instance = new singleton();
            return instance;
        }
};
singleton *singleton::instance = 0;

int main() {
    singleton *s1 = singleton::getInstance();
    singleton *s2 = singleton::getInstance();
    std::cout << s1 << '\n';
    std::cout << s2 << '\n';
    return 0;
}

您可以从输出中看到两个指针是相同的:

0xbc0358
0xbc0358

或参考版本,因为这似乎是您的目标:

#include <iostream>

class singleton {
    protected:
        static singleton *instance;
        singleton() { }
    public:
        static singleton& getInstance() {
            if (instance == 0)
                instance = new singleton();
            return *instance;
        }
};
singleton *singleton::instance = 0;

int main() {
    singleton &s1 = singleton::getInstance();
    singleton &s2 = singleton::getInstance();
    std::cout << &s1 << '\n';
    std::cout << &s2 << '\n';
    return 0;
}

For a start, I think:

singleton();

should be:

instance = new singleton();

The way you have it, you're not actually storing the newly instantiated object so instance will always be null.

It's also good form to explicitly set statics with:

singleton *singleton::instance = 0;

(outside the class definition).

In fact, it's possibly better to start with the baseline singleton code and work your way up from there. This is a standard-form pointer version:

#include <iostream>

class singleton {
    protected:
        static singleton *instance;
        singleton() { }
    public:
        static singleton *getInstance() {
            if (instance == 0)
                instance = new singleton();
            return instance;
        }
};
singleton *singleton::instance = 0;

int main() {
    singleton *s1 = singleton::getInstance();
    singleton *s2 = singleton::getInstance();
    std::cout << s1 << '\n';
    std::cout << s2 << '\n';
    return 0;
}

You can see that both pointers are the same from the output:

0xbc0358
0xbc0358

Or the reference version, since that seems to be what you're aiming for:

#include <iostream>

class singleton {
    protected:
        static singleton *instance;
        singleton() { }
    public:
        static singleton& getInstance() {
            if (instance == 0)
                instance = new singleton();
            return *instance;
        }
};
singleton *singleton::instance = 0;

int main() {
    singleton &s1 = singleton::getInstance();
    singleton &s2 = singleton::getInstance();
    std::cout << &s1 << '\n';
    std::cout << &s2 << '\n';
    return 0;
}
聽兲甴掵 2025-01-03 14:50:04

在定义文件中,您需要实例的定义:

singleton* singleton::instance = NULL;

如果您想在多个翻译单元中使用单例,则应该将定义与声明分开。

另外,通常的做法是没有初始化方法:

static singleton* getInstance()
{ 
    if(instance==NULL)
        instance = new singleton();
    return instance;
}

关于单例是好还是坏有很多讨论,普遍的共识是应该避免它们。您还应该检查一下它们。

In your definition file, you need the definition of instance:

singleton* singleton::instance = NULL;

You should separate your definition from your declaration if you want to use the singleton in multiple translation units.

Also, the way it's usually done, is not having an initialize method:

static singleton* getInstance()
{ 
    if(instance==NULL)
        instance = new singleton();
    return instance;
}

There are lots of discussions on SO whether singletons are good or bad, with the general consensus that they should be avoided. You should also check them out.

善良天后 2025-01-03 14:50:04

您需要定义静态成员变量instance。在全局范围内的某个位置(例如在类和 main 函数之间)添加以下行:

singleton *singleton::instance = 0;  // use 'nullptr' if your compiler support it

此外,您可以将实例的初始化放在 getInstance 中:

static singleton& getInstance()
{
    if (instance == 0)
        instance = new singleton;
    return *instance;
}

并删除初始化函数。

您可能还希望将构造函数设为私有而不是受保护,并使用私有复制构造函数和赋值函数来防止复制和赋值。

You need to define the static member variable instance. Somewhere in global scope (for example between the class and the main function) add the following line:

singleton *singleton::instance = 0;  // use 'nullptr' if your compiler support it

Also, you can put the initialization of the instance in getInstance:

static singleton& getInstance()
{
    if (instance == 0)
        instance = new singleton;
    return *instance;
}

and remove the initialize function.

You might also want to make the constructor private instead of protected, as well as having private copy-constructor and assignment functions to prevent copying and assignment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文