I have the following code:
#include <iostream>
using namespace std;
class Test {
public:
static Test& get() {
static Test testSing;
return testSing;
}
};
int main() {
Test a = Test::get();
Test b = Test::get();
cout << &a << endl;
cout << &b << endl;
}
I thought that a
and b
should have the same address, since I thought they should be constructed only once. However, I get different memmory addresses in this test.
Is there something trivial that I am missing? Shouldn't they have the same address?
You use the copy constructor, so you have 2 different objects, references are missing.
You probably want
Test& a = Test::get();
Test& b = Test::get();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句