将默认值设置为Singleton

时间:2018-03-26 13:10:35

标签: c++ static singleton gmock private-constructor

我使用 gmock框架获得 singleton 类进行测试覆盖。一些方法(如示例中的method1())在调用期间更改类的字段。有没有办法在没有修改TestingClass的情况下将默认值设置为类的字段?

示例:

class TestingClass
{

private:

TestingClass(): _field1(0)
              , _field2(0)
{

}
TestingClass(const TestingClass&){}
TestingClass & operator=(const TestingClass &){}

public:

static TestingClass Instance()
{
    static TestingClass instance;
    return instance;
}

void method1()
{
    _field1 = 777;
}
void method2()
{
    _field1 = 888;
}
void Show()
{
    std::cout << "\nField1 - " << _field1
              << "\nField2 - " << _field2
              << "\n";
}

//other methods...


private:

int _field1;
int _field2;

};

0 个答案:

没有答案