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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Copy Constructor in Singletonclass.

Copy Constructor in Singletonclass.

Scheduled Pinned Locked Moved C / C++ / MFC
question
5 Posts 3 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.
  • C Offline
    C Offline
    Cpp_Com
    wrote on last edited by
    #1

    Hi All, I ve a doubt. Is it necessary to override copy constructor inside a sigleton class? if Yes, why so? I c'd nt find a satisfactory answer in net, so thought to put it here. Thank u all.

    _ A 2 Replies Last reply
    0
    • C Cpp_Com

      Hi All, I ve a doubt. Is it necessary to override copy constructor inside a sigleton class? if Yes, why so? I c'd nt find a satisfactory answer in net, so thought to put it here. Thank u all.

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      A singleton class usually also controls its creation. This is normally done using a static method so that clients can call the static method to get hold of the one and only object instance of the class. To prevent clients from directly creating objects of the class, we make the constructors private. When the default constructor is private, clients will not be able to create an instance of the class as follows -

      CMySingletonClass Obj; // Error since default constructor is private.

      And when the copy constructor is private clients cannot instantiate the class as follows -

      CMySingletonClass AnotherObj(Obj); // Error since copy constructor is private.

      «_Superman_» I love work. It gives me something to do between weekends.
      Microsoft MVP (Visual C++)

      C 1 Reply Last reply
      0
      • C Cpp_Com

        Hi All, I ve a doubt. Is it necessary to override copy constructor inside a sigleton class? if Yes, why so? I c'd nt find a satisfactory answer in net, so thought to put it here. Thank u all.

        A Offline
        A Offline
        Avi Berger
        wrote on last edited by
        #3

        If you don't provide a copy constructor and write something that the compiler implements by making a copy of your object, the compiler can and will provide a default one and silently use it. Then you no longer have a singleton. Declare a private copy constructor and do not implement it. Now the compiler will not provide its own. In addition, if code outside the class tries to create a copy, you get a compile time error for trying to access a private function. Finally, if somehow code inside the class tries to make a copy, you get a link time error since the implementation is missing. What you don't get is an executable that is making copies of your "singleton" without you realizing it.

        1 Reply Last reply
        0
        • _ _Superman_

          A singleton class usually also controls its creation. This is normally done using a static method so that clients can call the static method to get hold of the one and only object instance of the class. To prevent clients from directly creating objects of the class, we make the constructors private. When the default constructor is private, clients will not be able to create an instance of the class as follows -

          CMySingletonClass Obj; // Error since default constructor is private.

          And when the copy constructor is private clients cannot instantiate the class as follows -

          CMySingletonClass AnotherObj(Obj); // Error since copy constructor is private.

          «_Superman_» I love work. It gives me something to do between weekends.
          Microsoft MVP (Visual C++)

          C Offline
          C Offline
          Cpp_Com
          wrote on last edited by
          #4

          Thank u superman for the reply. But my doubt is if i have a singletonclass, assume CMySingleTon, and inside the class i've static CMySingleton& GetInstance() methode whice is returning the static CMySingleTon obj. If i need the object, I've to call like CMySingleton::GetInstance();, and i cannt create an object outside class. My doubt is that when CMySingletonClass AnotherObj(Obj); // Error since copy constructor is private. will be called. And is it possible. Thank u all.

          _ 1 Reply Last reply
          0
          • C Cpp_Com

            Thank u superman for the reply. But my doubt is if i have a singletonclass, assume CMySingleTon, and inside the class i've static CMySingleton& GetInstance() methode whice is returning the static CMySingleTon obj. If i need the object, I've to call like CMySingleton::GetInstance();, and i cannt create an object outside class. My doubt is that when CMySingletonClass AnotherObj(Obj); // Error since copy constructor is private. will be called. And is it possible. Thank u all.

            _ Offline
            _ Offline
            _Superman_
            wrote on last edited by
            #5

            This is where it would be called -

            CMySingleton& obj = CMySingleton::GetInstance();
            CMySingleton AnotherObj(obj);

            «_Superman_» I love work. It gives me something to do between weekends.
            Microsoft MVP (Visual C++)

            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