C++: Implementing the "Singleton Pattern" with Static Members

After watching two or three days of online courses on the Internet, I learned about C++. I happened to encounter a problem with my homework. After I solved it, I felt it necessary to write it on the blog, so I posted it.
The question is: how to implement the "singleton pattern", which means that an object can only have one instance.

Ideas:
1. A counter is needed to record the number of instances, and the counter is incremented each time the constructor is successfully executed. Counters are static members.
2. Every time the constructor is called, first check whether the value of the counter is less than 1. If it is less than 1, execute the constructor. Otherwise, delete the object . 3. The way
to delete the object is to delete this;
One problem is that the object is released, but according to this idea, a released result cannot be returned, so there will be a situation in which the member function of the non-existing object is called during the program operation.

Solutions:
1. Declare a static pointer member, the type is the type of the object, and the initial value is NULL.
2. Set the accessibility of the constructor to --protected.
3. Declare a static function whose return value is an object pointer, and write an if statement internally. If the counter is less than 1, call the constructor new instance and assign it to the static pointer member and return the member, otherwise return directly.
In this way, there is no What's the problem? Since the constructor is not executed when trying to instantiate the object for the second time, there is no memory leak

Note: Since the constructor cannot be accessed directly, it needs to be called with "class name::function name()" when constructing an instance. Please refer to the main function code for details.

code show as below:

Object declaration header file:

#pragma once
#include <iostream>
class People
{
    protected:
        static int num;
        static People* people;
        int id;
        People(int id);
    public:
        static People* getObject(int id);
        int getId();
        static int getNum();
};

Object implementation file:

#include "People.h"

int People::num = 0;
People* People::people = NULL;

People* People::getObject(int id)
{
    if (num < 1)
    {
        people = new People(id);
    }
    return people;
}

People::People(int id)
{
    this->id = id;
    num++;
}

int People::getId()
{
    return id;
}

int People::getNum()
{
    return num;
}

Main function:

#include <iostream>
#include <Windows.h>
#include "People.h"

using std::cout;
using std::endl;

int main()
{
    People *p1 = People::getObject(1);
    cout << "对象指针p1所指的对象的id = " << p1->getId() << "此时对象的实例数num = " << People::getNum() << endl << endl;

    People *p2 = People::getObject(2);
    cout << "对象指针p2所指的对象的id = " << p1->getId() << "此时对象的实例数num = " << People::getNum() << endl << endl;

    People *p3 = People::getObject(3);
    cout << "对象指针p3所指的对象的id = " << p1->getId() << "此时对象的实例数num = " << People::getNum() << endl << endl;

    People *p4 = People::getObject(4);
    cout << "对象指针p4所指的对象的id = " << p1->getId() << "此时对象的实例数num = " << People::getNum() << endl << endl;

    People *p5 = People::getObject(5);
    cout << "对象指针p5所指的对象的id = " << p1->getId() << "此时对象的实例数num = " << People::getNum() << endl << endl;

    People *p6 = People::getObject(6);
    cout << "对象指针p6所指的对象的id = " << p1->getId() << "此时对象的实例数num = " << People::getNum() << endl << endl;

    system("pause");
}

operation result:
write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325798688&siteId=291194637