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 6351923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:06:12+00:00 2026-05-24T22:06:12+00:00

In the Singleton pattern why is that we have to use a static object

  • 0

In the Singleton pattern why is that we have to use a static object and not a global object?

I tried the following

class Singleton;
Singleton*  instance1 = NULL;

class Singleton
{
private :
       Singleton()
       {
       }
       //static Singleton* instance1;

public:
        static Singleton* getinstance()

 if(instance1== NULL)
       {
       instance1 = new Singleton();
       }

 return instance1;


 void Dispaly()
       {
       }
 ~Singleton()
       {
       }
};

When I compile this code I get the error “multiple definition of instance1”

Can anyone please give a plausible reason for this?

  • 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-05-24T22:06:13+00:00Added an answer on May 24, 2026 at 10:06 pm

    If you make it a global object, then everyone has access to it, and anyone can use it without calling getInstance(). If so, then what is the purpose of getInstance() then? First time, you will call it to create the instance, then you wouldn’t be required to call getInstance(), since after the first call, you can directly use instance.

    A private static instance gives you more control how it can be accessed: only through by calling getInstance().

    Now why you get multiple definition of instance error when compiling your code? Its because you’ve defined the global object in the header file itself, which is included in multiple .cpp files. This causes multiple definitions of the object, one definition in each translation unit (.obj file).

    What you actually should be doing is this:

    //Singleton.h
    class Singleton
    {
    private :
            Singleton();
            Singleton(const Singleton &);
           ~Singleton();
    
            static Singleton* instance; //declaration of static member!
    
    public:
            static Singleton* getInstance();
           //..
    };
    

    And then define the static member in the .cpp file as:

    //Singleton.cpp
     Singleton *Singleton::instance = 0; //definition should go in .cpp file!
    
     Singleton::Singleton() {}
    
     Singleton* Singleton::getInstance()
     {
         if ( instance  == 0 ) instance  = new Singleton();
         return instance;
     }
     //...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Difference between static class and singleton pattern? we use static class for
is it good, that every PHP class implements a Singleton pattern? I think, it
I have an object cache which implements the Singleton design pattern. My approach to
I'm implimenting my own ApplicationContext class that uses the singleton pattern. I want to
I have a class ( MyService ) that has a static property ( MyService.Context
I've learned that to have a singleton lazy loading, this is the pattern to
I have decided to use the singleton pattern for my application. It makes the
I have an error log class that I use throughout all the files in
The singleton pattern is a design pattern that is used to restrict instantiation of
I'm using a singleton pattern for the datacontext in my web application so that

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.