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

  • Home
  • SEARCH
  • 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 8763389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:46:15+00:00 2026-06-13T15:46:15+00:00

I have implemented two methods of how I think a Singleton class should be

  • 0

I have implemented two methods of how I think a Singleton class should be implemented, I just want the opinion of programmers to which one is the best method to use.

Each of the methods uses these classes:

class Animal {
public:
    virtual void speak() const = 0;
};

class Dog {
    virtual void speak() { cout << "Woof!!"; }
};

First method:

class AnimalFactory {
public:
    static Animal* CreateInstance(int theTypeOfAnimal);

private:
    AnimalFactory() { };
    static int count; // this will be used to count the number of objects created
    static int maxCount; // this is the max count allowed.
};

int AnimalFactory::count = 0;
int AnimalFactory::maxCount = 1;

Animal* AnimalFactory::CreateInstance(int theTypeOfAnimal)
{
    Animal* pAnimal = NULL;

    if(pAnimal != NULL)
    {
        return pAnimal;
    }

    switch(theTypeOfAnimal)
    {
        case 0:
            pAnimal = new Dog();
            count++;
            break;

        case 1:
            pAnimal = new Cat();
            count++;
            break;

        case 2:
            pAnimal = new Spider();
            count++;
            break;

            default:
            cout << "Not known option";
    }

    return pAnimal;
}

Second Method:

template<typename classType>
class Singleton {
public:
    classType& instance()
    {
        static classType object;
        return object;
    }
};

Any opinion would be grateful, 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-13T15:46:16+00:00Added an answer on June 13, 2026 at 3:46 pm

    The first code snippet fails to implement a singleton because AnimalFactory::CreateInstance fails to check the instance count that it maintains.

    The second code snippet,

    template<typename classType>
    class Singleton {
    public:
        classType& instance()
        {
            static classType object;
            return object;
        }
    };
    

    when done properly, is called a Meyers’ singleton, after Scott Meyers.

    The code as given would work but yields awkward usage. To avoid having to instantiate Singleton, the instance method should be declared static. Also, the requirement on classType classes that they should only instantiable by Singleton, should be documented, e.g. as a comment with an example.

    Others have already warned you against using singletons, but let me also do that.

    Singletons have their uses, such as ensuring that e.g. a Windows API “window class” is only created once, but mainly they’re abused as global-variables-in-disguise. One problem with global variables, namely that you don’t know when a global variable has been initialized (in C++ programming known as the static initialization order fiasco), is avoided by singletons. However, the main problem, that they serve as spaghetti communication systems, routing chaos-inducing information in untraceable ways between places you would never suspect, is still there.

    So, don’t.

    But if you at some time find that you absolutely need singletons, e.g. for a logging facility, then please do read up on the discussion of singletons in Andrei Alexandrescu’s “Modern C++ Design”, and then get hold of the Loki library and let it do the job for you.

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

Sidebar

Related Questions

For example, I have a class that has two strings, one of which must
I have implemented two classes, isbn10 and isbn13. I would like to create a
I have implemented inheritance for two parent classes called Table and Field . Each
i have two int array of images , i have implemented it in grid
I have implemented a list view. Each item in my list view has two
I have implement a scenario which involves two way communication between child and parent
I have two projects in my Solution. One implements my business logic and has
I have two entities A and B: A.java: ... public class A implements Serializable
I have this Two Methods private function cacheAdd($id,$data) { $this->minicache->set($id,$data); } private function cacheGet($id)
I have the following two methods on an interface for my service layer: ICollection<T>

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.