简体   繁体   English

请解释一个功能单例(C ++)

[英]Please, Explain a one of feature singleton(C++)

For example I have a following class: 例如,我有一个以下课程:

class singelton
{
public:
    static singelton* Instance()
    {
        if (m_pInstance == 0)
        {
            m_pInstance = new singelton();
        }
        return m_pInstance;
    }

    void setData(std::string input) { data = input; }
    void getData() const { std::cout << data << std::endl; }
private:
    singelton() {}
    ~singelton() {}

    static singelton* m_pInstance;

    std::string data;
};

typedef singelton s;

//what is this? Why need a singleton name? I mean "singelton*".
singelton* singelton::m_pInstance = 0;

int main(int argc, char** argv)
{
    s.Instance()->setData("Something...");
    s.Instance()->getData();
    return 0;
}

What is singelton* singelton::m_pInstance = 0; 什么是singelton* singelton::m_pInstance = 0; ? This function assigns zero/null to a singleton instance, but why need use singleton* ? 该函数为单例实例分配零/空值,但是为什么需要使用singleton* That assignment like a function, but use as assignment. 该分配就像一个函数,但用作分配。

Static data members are not part of objects of a given class type; 静态数据成员不是给定类类型的对象的一部分; they are separate objects. 它们是独立的对象。 As a result, the declaration of a static data member is not considered a definition. 结果,静态数据成员的声明不被视为定义。 So static member must be defined outside of the class declaration. 因此,必须在类声明之外定义静态成员。

In your example: 在您的示例中:

  • singelton * is a type of member. singelton *是一种成员。
  • singleton:: is class name (like namespace) singleton::是类名(如名称空间)
  • and m_pInstance is member name 并且m_pInstance是成员名称

PS: Because of static variables are initialised with 0 by default in C++, you don't need to explicitly set m_pInstance to 0 (or NULL ). PS:由于静态变量在C ++中默认情况下初始化为0,因此无需将m_pInstance显式设置为0 (或NULL )。 The definition only will be enough: 仅定义就足够了:

singelton * singelton::m_pInstance;

What is singelton* singelton::m_pInstance = 0; 什么是singelton* singelton::m_pInstance = 0; ?

It's the initialiser for the static member variable m_pInstance , statically initialising the pointer to be null. 它是静态成员变量m_pInstance初始化程序,将指针静态初始化为null。 singelton* (pointer to singelton) is the type of the variable; singelton* (指向singelton的指针)是变量的类型; singlelton::m_pInstance is the (qualified) name of the variable; singlelton::m_pInstance是变量的(限定)名称; and = 0 is the initialiser. = 0是初始化程序。

This function assigns zero/null to a singleton instance, but why need use singleton* ? 该函数为单例实例分配零/空值,但是为什么需要使用singleton*

No, it initialises the pointer to null; 不,它将指针初始化为null; it doesn't point to anything yet. 它还没有指向任何东西。 The object itself will be created, and the pointer updated to point to it, the first time someone calls Instance() . 第一次有人调用Instance()时,将创建对象本身,并更新指针以指向该对象。 It's a pointer so that the object itself can be created when it's first needed, rather than at some arbitrary point during program startup - this is known as "lazy initialisation". 它是一个指针,使对象本身可以在首次需要时创建,而不是在程序启动期间的任意点创建-这称为“惰性初始化”。

Beware that, in C++, there is no way to implement the Singleton anti-pattern correctly. 请注意,在C ++中,无法正确实现Singleton反模式。 This particular implementation has the problems of leaking the object, and not being thread-safe. 该特定实现具有泄漏对象并且不是线程安全的问题。 I strongly recommend that you get rid of it: just instantiate the object in an appropriate place, with a lifetime that's longer than whatever uses it, and pass references to whatever needs it. 我强烈建议您摆脱它:只需在适当的位置实例化该对象,并使其寿命比使用该对象的对象更长,然后将其传递给需要它的对象。

Use 采用

Singleton::Instance()->setData("Hello");

and

Singleton::Instance()->getData();

The class can only have one instance - hence called singleton 该类只能有一个实例-因此称为单例

And the Singleton::Instance gives you access to that Singleton::Instance可让您访问

singelton* singelton::m_pInstance = 0;

Initializes it and when you first use it the singleton is created 对其进行初始化,并在首次使用时创建单例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM