【发布时间】:2021-05-05 16:55:16
【问题描述】:
我想知道下面的代码是否有效。
在 main 函数中,将设置 'testClass' 上的成员的 'testValue'。 如果通过调用 'showTestValue()' 方法打印,必须更新变量吗? 或者在某些情况下变量可能不会更新?
#include <stdio.h>
#include <stdlib.h>
class testClass {
public:
int testValue;
static testClass* getInstance();
void showTestValue();
private:
testClass();
};
testClass*
testClass::getInstance()
{
static testClass ins;
return &ins;
}
testClass::testClass()
{
testValue = 55;
}
void
testClass::showTestValue()
{
printf("testVal is :%d\n", this->testValue);
}
int main(void)
{
for(int i = 0; i < 100; ++i)
{
testClass::getInstance()->testValue = i;
testClass::getInstance()->showTestValue();
}
exit(13);
}
【问题讨论】:
-
您关心代码的哪一部分无效?您认为此代码与您通常设置或打印类成员的方式有何不同?
标签: c++ design-patterns architecture singleton