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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:37:38+00:00 2026-05-18T01:37:38+00:00

I did see on internet some pattern for implementing singleton class as following: class

  • 0

I did see on internet some pattern for implementing singleton class as following:

class singleton {
public:
    int a;
    void print()
    {
        std::cout << a << std::endl;
    }
} singleton;

int main()
{
    // singleton b; COMPILATION ERROR
    singleton.a = 3;
    singleton.print();

    return 0;
}

It does compile on GCC but I got the feeling that naming the instance as the name of the class is not really a proper way to code.

  • Is there any special rules which forbid such convention (instance name same as the class name)
  • Is the source code compiling in other compiler ?
  • 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-18T01:37:38+00:00Added an answer on May 18, 2026 at 1:37 am

    Regarding the two question that you literally ask,

    • yes, C++ has the notion of names of different kinds co-existing in the same scope, as in C, and

    • yes, when the appropriate header is included the code compiles fine with MinGW g++ 4.4.1, Visual C++ 10.0, and Comeau Online 4.3.10.1.

    When you’re in doubt about whether some code snippet is standard-conforming, as a practical matter just submit it to Comeau Online.

    That the code is technically OK does not, however, mean that it’s OK… 🙂

    Singleton is used to describe something different than a global variable. What you have is a global variable. Ergo, it’s not a singleton.

    Two basic properties of a singleton are

    • there is globally unique instance, and

    • that instance is created lazily, on demand.

    Usually one wants something similar to a global variable but more controlled. One wants a more controlled instantiation order, and the lazy creation helps with that. And one wants to limit the number of allowed instances, usually to

    • just one single instance.

    The latter is so common a reason for using singletons that e.g. Wikipedia’s article on singletons describes a singleton as “restricting the instantiation of a class to one object”. But that’s a simplistic view. Sometimes one needs a specific number of instances, as singletons. And e.g. for Python it’s not uncommon to describe Python’s False and True values as singletons of the bool type. And sometimes one needs a singleton (or singletons) while still allowing arbitrary instantiation.

    In C++ one particularly easy way to implement a singleton is known as Meyers’ Singleton. Essentially the singleton instance is then a static variable within the function that provides access to it. This is usually combined with prohibiting general instantiation of the class, e.g. by the constructor or all constructors private, and the singleton access function a static member function.

    Cheers & hth.,

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

Sidebar

Related Questions

I am looking over some code someone else did and I see this: if
Did anyone try to read programmatically an Alibre Design CAD file? I see that
Did anyone try that feature and has some feedback? Or Does anyone know some
I am having some Internet Explorer 7 (and 8 beta) browser-specific problems with a
I'm at a stage in which I did not learn internet scripting languages yet,
Trying to compile a sample http class with the SDK, and getting some strange
I would like to check to see if I have an Internet connection on
I did see this to modify CFileDialog ( http://support.microsoft.com/kb/105497 ) but it looks like
Did you ever have to choose between WISA or LAMP at the beginning of
Did you ever had a bug in your code, you could not resolve? I

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.