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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:43:52+00:00 2026-05-21T03:43:52+00:00

I have a question with singlton. Where should I declare the static member of

  • 0

I have a question with singlton.

Where should I declare the static member of the singleton class?

why not working like this

class singleton{

  private:

  static singleton & m_singleton;

  public:

  static singleton& get_instance{

          return m_singleton;
  }

}

but I have to be like this

class singleton{

  public:

  static singleton& get_instance{

     static singleton & m_singleton;

          return m_singleton;
  }

}

What’s the differnece?

I know there is another way to use pointer, but now I am only talking about the case to use an object.

Also another questions, what’s the pros/cons to use pointer and reference for singleton ?

Thanks so much!

  • 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-21T03:43:53+00:00Added an answer on May 21, 2026 at 3:43 am

    In the first case, the singleton is constructed (constructor called) when the program initializes, before main() is called. In this case you will also need to define the static variable somewhere in your cpp file.

    In the second case the singleton is constructed when the function is first called. Mind you that this implementation is not thread safe since static variable initialization in functions is not thread safe. If two threads call this function for what appears to be the first time, there is a small chance you’ll end up with two singletons.

    Also notice you have a bug in the second one. You cannot define a reference without an initalization. This

    static SomeClass &var;
    

    Does not compile. You need to remove the reference to create an actual instance of the class, not a reference, and then return a reference to it.

    If in the second example you define the static variable as a pointer you can avoid the threading problem I mentioned by carefully initializing the pointer. Read more about it in this class article (that talks about Java but the core issue is the same)

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

Sidebar

Related Questions

Okay, newbie multi-threading question: I have a Singleton class. The class has a Static
I have a question, is this the correct approach to make a Generic Singleton?
I have a singleton class with this code: manager.h @interface Manager : NSObject {
I have a question about the singleton pattern. I saw two cases concerning the
I have question regarding the SQLAlchemy. How can I add into my mapped class
I have question concerning a function I created. I would like to show the
I'm learning EF now and have a question regarding the ObjectContext: Should I create
Say that I have a singleton class (Downloader) responsible for downloading and persisting files.
I have a Singleton class here designed to be inherited by classes wanting to
Basically I have a class which an instance of is created via a Singleton

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.