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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:02:56+00:00 2026-05-26T15:02:56+00:00

Below is the code for a singleton class class single{ private: int i; single(int

  • 0

Below is the code for a singleton class


class single{
private:
    int i;
    single(int x): i(x){ 

    }
public:
    static single& getInstance(){
        static single s(10);
        return s;
    }

    void incPrint(){
        ++i;
        cout i  " ";
    }
};

Now the same code gives two different results for 2 different code


single d1 = single::getInstance();
    d1.incPrint();
    d1.incPrint();

    single d2 = single::getInstance();
    d2.incPrint();
    d2.incPrint();

This code produces the output:

11
12
11
12

Whereas this code


    single & d1 = single::getInstance();
    d1.incPrint();
    d1.incPrint();

    single & d2 = single::getInstance();
    d2.incPrint();
    d2.incPrint();

produces the result

11 12 13 14

where the latter is the desired output. Is this anomaly because of design issues or user coding issues ? How to make sure only the second result is obtained ?

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-05-26T15:02:57+00:00Added an answer on May 26, 2026 at 3:02 pm

    T x = foo() makes a copy.

    Or behaves "as if" it made a copy.

    While T& x = foo(), when foo returns a T& reference, only makes a reference.


    Make the copy constructor and copy assignment operator private. That will prevent people from making copies all. Making the copy assignment operator private prevents self-copying.

    It is not an alternative to return a pointer to the singleton from getInstance. That would be a surefire way to let people be unaware of the guarantee that there’s always an instance. A pointer indicates that it can be a nullpointer.

    The best alternative is, however, to not use singletons if you can avoid it. There are many problems with singletons. These problems include lifetime management, thread safety and interaction with tools that detect memory leaks.

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

Sidebar

Related Questions

i have the code as below : class Singleton { private: int i; static
The below code in Java throws Null pointer exception. public class New{ int i;
I have a MySQL singleton class written in PHP. Its code is listed below:
Below code instantiates a derived singleton object based on environment variable. The compiler errors
How below 2 codes are different in terms of multithreaded environment? Code 1: public
the code below is how i implement singleton with metaclass, and it works well
Below code saying error incorreect syntax near Main INSERT INTO tbl ( 'Week', Main,
The below code is very simple. I have a jQuery autocomplete bound to an
My below code works fine and is used to populate a <select> item with
The below code works when there are a few element in the To list

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.