Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何限制C++中要创建的对象的数量?_C++_Singleton_Static Methods_Static Members - Fatal编程技术网

如何限制C++中要创建的对象的数量?

如何限制C++中要创建的对象的数量?,c++,singleton,static-methods,static-members,C++,Singleton,Static Methods,Static Members,我正在编写一个示例代码,以便从一个类中仅创建5个对象。我已经这样写了我的代码 #include <iostream> using namespace std; class SingletonGeneric { private: static int Count; static SingletonGeneric *single; SingletonGeneric() { //private constructor } publ

我正在编写一个示例代码,以便从一个类中仅创建5个对象。我已经这样写了我的代码

#include <iostream>

using namespace std;

class SingletonGeneric
{
private:
    static int Count;
    static SingletonGeneric *single;
    SingletonGeneric()
    {
        //private constructor
    }
public:
    static SingletonGeneric* getInstance();
    void method();
    ~SingletonGeneric()
    {
        Count -- ;
    }
};

int SingletonGeneric::Count = 0;
SingletonGeneric* SingletonGeneric::single = NULL;
SingletonGeneric* SingletonGeneric::getInstance()
{
    if( Count >= 0 && Count < 6)
    {
        single = new SingletonGeneric();
        Count = ++;
        return single;
    }
    else
    {
        return single;
    }
}

void SingletonGeneric::method()
{
    cout << "Method of the SingletonGeneric class" << endl;
}
int main()
{
    SingletonGeneric *sc1,*sc2;
    sc1 = SingletonGeneric::getInstance();
    sc1->method();
    sc2 = SingletonGeneric::getInstance();
    sc2->method();

    return 0;
}

但我没有得到预期的结果。所以请告诉我应该如何修改我的代码。或者,如果有其他简单的方法,请告诉我。

quick response Count=++;是语法错误。您如何测试它?你的期望是什么?我想这会对你有所帮助。常用的方法是在你的SingletonGeneric构造函数中设置count+,在你的SingletonGeneric Destructor中设置count-。还要检查SingletonGeneric构造函数中的计数<5,如果计数>=5,则抛出一个异常。
#include <iostream>

using namespace std;

class SingletonGeneric
{
private:
    static int Count;
    static SingletonGeneric *single;
    SingletonGeneric()
    {
        //private constructor
    }
public:
    static SingletonGeneric* getInstance();
    void method();
    ~SingletonGeneric()
    {
        Count -- ;
    }
};

int SingletonGeneric::Count = 0;
SingletonGeneric* SingletonGeneric::single = NULL;
SingletonGeneric* SingletonGeneric::getInstance()
{
    if( Count >= 0 && Count < 5) // should be 5 not 6
    {
        single = new SingletonGeneric();
        Count++;
        return single;
    }
    else
    {
        return NULL;// not the old single
    }
}

void SingletonGeneric::method()
{
    cout << "Method of the SingletonGeneric class" << endl;
}
int main()
{
    SingletonGeneric *sc1,*sc2, *sc3, *sc4, *sc5, *sc6;
    sc1 = SingletonGeneric::getInstance();
    sc1->method();
    sc2 = SingletonGeneric::getInstance();
    sc2->method();
    sc3 = SingletonGeneric::getInstance();
    sc3->method();
    sc4 = SingletonGeneric::getInstance();
    sc4->method();
    sc5 = SingletonGeneric::getInstance();
    sc5->method();
    sc6 = SingletonGeneric::getInstance();
    if (sc6 != NULL) {
        sc5->method();
    } else {
        cout << "only have to create 5 objects" << endl;
    }

    return 0;
}