1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| class Single { public: static Single *Get() { static Single re; return &re; } void Set(int x, int y) { a = x; b = y; }
void Print() { std::cout << a << " " << b << std::endl; }
private: int a = 0; int b = 0; Single() {} Single(const Single &) {} Single &operator=(const Single &) {} };
int main() { auto s = Single::Get(); s->Set(12, 13); s->Get()->Print(); std::thread([]() { auto sa = Single::Get(); sa->Set(01, 3); }).join(); s->Get()->Print(); }
// output 12 13 1 3
|