This is my singleton
class MySingleton {
private:
int myNumber;
public:
static MySingleton &get() {
static MySingleton instance;
return instance;
}
int getMyNumber() const {
return myNumber;
}
void setMyNumber(int myNumber) {
MySingleton::myNumber = myNumber;
}
};
and this is the code to test it
MySingleton t1 = MySingleton::get();
t1.setMyNumber(6);
printf("t1: %d \n", t1.getMyNumber());
MySingleton t2 = MySingleton::get();
printf("t2: %d \n", t2.getMyNumber());
Now the output I get is
t1: 6
t2: 0
but te expected result would be
t1: 6
t2: 6
It looks like MySingleton::get()
is creating a new instance, something it shouldn't do of course.
Aucun commentaire:
Enregistrer un commentaire