Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8833797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:52:04+00:00 2026-06-14T08:52:04+00:00

The following code is my implementation of the Singleton Pattern. #include <iostream> template<class T>

  • 0

The following code is my implementation of the Singleton Pattern.

 #include <iostream>

template<class T>
class Uncopyable
{
protected:
    Uncopyable(){}
    ~Uncopyable(){}
private:
    Uncopyable(const Uncopyable<T>&);
    Uncopyable& operator=(const Uncopyable<T>&);
};

template <class T>
class Singleton : private Uncopyable<T>
{
public:
    static T* getInstancePtr()
    {
        return instance;
    }
protected:
    Singleton<T>()
    {
        if(instance == 0)
        {
            instance = new T();
        }
    };
    ~Singleton<T>()
    {

    };
private:
    static T* instance;
};
template<class T> T* Singleton<T>::instance = 0;

class Test : public Singleton<Test>
{
public:
    Test(){};
    ~Test(){};
    inline void test() const
    {
        std::cout << "Blah" << std::endl;
    }
private:
    friend class Singleton<Test>;
protected:
};

int main(int argc, char* argv[])
{
    Test* t = Test::getInstancePtr();
    Test* t2 = Test::getInstancePtr();

    t->test();
    t2->test();

    return 0;
}

It works in this form, however I am uncertain as to whether it really is correct due to the constructor and destructor of the Singleton being protected as opposed to being private. If I declare them as private the code will not compile as they are not accessible to the class. Is this implementation safe to use, or is there anything I can do to improve it to ensure only one instance will be created and used.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:52:05+00:00Added an answer on June 14, 2026 at 8:52 am

    There are several things wrong with the code you’ve posted.

    1. The Uncopyable class doesn’t need to be templated
    2. The Singleton class isn’t thread safe
    3. Your Singleton instance is never deleted

    I would re-implement your accessor as:

    static T& GetInstance()
    {
        static T instance;
        return instance;
    }
    

    Then make sure you call Singleton<T>::GetInstance() in the main thread of your application (during initialisation) to avoid any threading issues.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code implementation of my generic singleton provider: public sealed class
Following code worked fine abstract class FunctionRunnable<V> implements Runnable { protected abstract V calculate();
I have the following code: #include <iostream> #include <vector> #include <algorithm> int main( int
The following code seems to always follow the true branch. #include <map> #include <iostream>
I'm using a Singleton class and following is the code: .h File: #import <Foundation/Foundation.h>
The following code sample is an implementation of the Strategy pattern copied from Wikipedia
I have the following code to my UIViewController class implementation: - (void)viewDidLoad { CustomUITableView
I have the following code inside my @interface FriendsNavController : UINavigationController class implementation. The
i have the following code, taken partly from a Red Black Tree Java implementation.
I have an implementation of lazyDataModel following this tutorial http://uaihebert.com/?p=1120 My code its a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.