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. controlling the creation of objects

controlling the creation of objects

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
3 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.
  • K Offline
    K Offline
    kumar_mk
    wrote on last edited by
    #1

    dear members, Is there any way i can control the creation of the objects for a class, like for example we have a singleton class for which we can create only a single instance, likewise i wanna limit the creation of objects say only 2 objects for a class or only 3 object for the class please give a small code example if possible. thank u in advance kumar_windows

    _ N 2 Replies Last reply
    0
    • K kumar_mk

      dear members, Is there any way i can control the creation of the objects for a class, like for example we have a singleton class for which we can create only a single instance, likewise i wanna limit the creation of objects say only 2 objects for a class or only 3 object for the class please give a small code example if possible. thank u in advance kumar_windows

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

      The simple way Create a static variable and in the contructor of the class keep on checking the value of it. If it exceeds the value (2 or 3 - exit) ex: class TestClass { static int i; public : TestClasss() { if(i>=3) exit(0); else i++; } // Carry on with your code }; Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_

      1 Reply Last reply
      0
      • K kumar_mk

        dear members, Is there any way i can control the creation of the objects for a class, like for example we have a singleton class for which we can create only a single instance, likewise i wanna limit the creation of objects say only 2 objects for a class or only 3 object for the class please give a small code example if possible. thank u in advance kumar_windows

        N Offline
        N Offline
        Nibu babu thomas
        wrote on last edited by
        #3

        kumar_windows wrote:

        Is there any way i can control the creation of the objects for a class, like for example we have a singleton class for which we can create only a single instance, likewise i wanna limit the creation of objects say only 2 objects for a class or only 3 object for the class

        You can have a static variable to count the number of instances created. Increment variable in the constructor decrement in the destructor. Hence keeping track of the objects created. The caveat is that you cannot entirely prevent the creation of an object but you can throw exceptions if object count increases beyond the limit that you have set.


        Nibu thomas A Developer Programming tips[^]  My site[^]

        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