Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Singleton Class

Singleton Class

Scheduled Pinned Locked Moved C / C++ / MFC
question
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pix_programmer
    wrote on last edited by
    #1

    Hi! Can anybody explain me why the constructor of a singleton class is declared as private? What would happen if it is not declared as private?

    R C S 3 Replies Last reply
    0
    • P pix_programmer

      Hi! Can anybody explain me why the constructor of a singleton class is declared as private? What would happen if it is not declared as private?

      R Offline
      R Offline
      RaviRanjanKr
      wrote on last edited by
      #2

      pix_programmer wrote:

      why the constructor of a singleton class is declared as private?

      because out of the class, object could not be instantiated

      pix_programmer wrote:

      What would happen if it is not declared as private?

      If you leave its constructor as public, the class won't be singleton anymore! Navigate the Given link to know about singleton in Details Singleton pattern [^] Hope It will works for you. :)

      1 Reply Last reply
      0
      • P pix_programmer

        Hi! Can anybody explain me why the constructor of a singleton class is declared as private? What would happen if it is not declared as private?

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        To forbid anybody to create an instance of the class. The idea of a singleton (as its name suggest) is that only one instance of the class exist. If you have a public constructor, it means that anybody can create instances of the class. When the constructor is declared public, if you try to instantiate the class outside of itself, the code will generate an error. So, if you make the constructor public, nothing will "happen" directly but your design is flawed: you are not sure anymore that your class can be instantiated multiple times.

        Cédric Moonen Software developer
        Charting control [v3.0] OpenGL game tutorial in C++

        1 Reply Last reply
        0
        • P pix_programmer

          Hi! Can anybody explain me why the constructor of a singleton class is declared as private? What would happen if it is not declared as private?

          S Offline
          S Offline
          Stefan_Lang
          wrote on last edited by
          #4

          As others have pointed out, the idea is that the implementation of a singleton class has full control over the property of ever having only one instance. So you need to make sure that there is no way to create a second instance, if one is already in existance, meaning: 1. You can not create an instance with a constructor without going through the class's code 2. You can not copy an (i. e. the only existing) instance to make another one 3. You can access the one existing instance or create it from anywhere The first is achieved by not providing a constructor that is public. The problem however is, that the C/C++ compiler implicitely always creates a default constructor if there is no other, and that default constructor would be dclared as public! Therefore we mus explicitely declare a default constructor that is private to prevent the compiler from creating a public one. The second would be achieved by not creating a copy constructor, but again, the compiler implicitely creates one if there is no other. The safest way to prevent this is again, to explicitely define a private copy constructor. The third can be achieved by defining a static function that delivers a reference or pointer to the one instance in existance. This function can check whether there already is an instance and retrun that, or if there is none, use the private constructor to create it.

          S 1 Reply Last reply
          0
          • S Stefan_Lang

            As others have pointed out, the idea is that the implementation of a singleton class has full control over the property of ever having only one instance. So you need to make sure that there is no way to create a second instance, if one is already in existance, meaning: 1. You can not create an instance with a constructor without going through the class's code 2. You can not copy an (i. e. the only existing) instance to make another one 3. You can access the one existing instance or create it from anywhere The first is achieved by not providing a constructor that is public. The problem however is, that the C/C++ compiler implicitely always creates a default constructor if there is no other, and that default constructor would be dclared as public! Therefore we mus explicitely declare a default constructor that is private to prevent the compiler from creating a public one. The second would be achieved by not creating a copy constructor, but again, the compiler implicitely creates one if there is no other. The safest way to prevent this is again, to explicitely define a private copy constructor. The third can be achieved by defining a static function that delivers a reference or pointer to the one instance in existance. This function can check whether there already is an instance and retrun that, or if there is none, use the private constructor to create it.

            S Offline
            S Offline
            Sauro Viti
            wrote on last edited by
            #5

            Very good explaination! Deserves a 5!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups