0

Suppose I have the following singleton base class:

template <class Derived>
class Singleton
{
  public:

    inline static Derived* get();                       

  protected:

    Singleton();                                  
    ~Singleton();                                     
    static std::unique_ptr<Derived> _uptr;                

  private:

    Singleton(const Singleton&);                          
    Singleton & operator = (const Singleton&);             
};

// Initialize the singleton object.
template <class Derived>
std::unique_ptr<Derived> Singleton<Derived>::_uptr = nullptr;

// Constructor
template <class Derived>
Singleton<Derived>::Singleton() {
}

// Constructor
template <class Derived>
Singleton<Derived>::~Singleton() {
}

// Function: get
template <class Derived>
inline Derived* Singleton<Derived>::get() {
  if(_uptr != nullptr) return _uptr.get();
  std::unique_ptr<Derived> tmp(new Derived());
  _uptr = std::move(tmp);
  return _uptr.get();
}

And I have the following derived classes:

class Foo : public Singleton <Foo> {
  public:
    int value;     
}

int main() {
  Foo A;
  Foo B;
  A.value = 1;
  B.value = 2;
  A.get()->value = 3;
  cout << A.value << endl;  // we get 1
  cout << B.value << endl;  // we get 2
  cout << A.get()->value() << " " << B.get()->value() << endl;  // we get 3
}

I am wondering why the three methods give completely different outputs on value. Shouldn't A and B return the same value as they inherit the same singleton base? To be more specific, I wanna implement a class that has global scope and will be initiated only once.

1
  • Foo A; Foo B; A Singleton where you have 2 instances actually, isn't that contradictory? I think you have some serious misconceptions here. You should actually prevent, that more than a single instance of that class can be created. Commented Nov 30, 2015 at 18:58

3 Answers 3

1

You still need to explicitely delete the (automatically created) default constructors and assignment operators in your derived Singleton classes to prohibit instantiation of more than one instance:

class Foo : public Singleton <Foo> {
public:
    int value;
    delete Foo();
    delete Foo(const Foo&);
    delete Foo& operator(const Foo&);
}
Sign up to request clarification or add additional context in comments.

Comments

0

I think you have misunderstood the singleton-pattern.

It is called singleton because only one instance is allowed. This is done by making the constructor private so that you can only create the instance via the get-function.

Consequently Foo A and Foo B would be illegal.

You get wrong values because you create 3 instances - that is not a singleton.

See https://en.wikipedia.org/wiki/Singleton_pattern

Comments

0

Your class Foo has a member variable int value, and also a static member variable std::unique_ptr<Foo> _uptr.

A.value = 1;        // now A.value == 1 and _uptr == nullptr
B.value = 2;        // now B.value == 2 and _uptr == nullptr
A.get()->value = 3; // now _uptr points to a Foo whose value is 3

There are three Foos, and three values.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.