CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Apr 2005
    Posts
    1,828

    Thumbs up Stop an object from being created

    How can we put a validation that an object of a class should'nt be created more than 10 times? can someone give me an example?

  2. #2
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496

    Re: Stop an object from being created

    You can use something like this ( a singleton like algorithm):

    Code:
    class CountedObj
    {
    private:
        CountedObj() { ...}
        CountedObj( const CountedObj&) { ...}
        ~CountedObj() { ...}
    
    public:
    
       static CountedObj* CreateObject()
       {
            if ( instances == 10)
                    return NULL;
           
            ++instances;
            return new CountedObj();
       }
    
       static void ReleaseObject( CountedObj* obj)
       {
            --instances;
            delete obj;
       }
    
    private:
       static instances;
    };

    You can transfer all the args of the constructor to overloaded versions of the CreateFunction.

    NOTE: if you plan to use something similar in a multithreaded environment, make sure you add the appropriate safety mechanisms.
    Last edited by PadexArt; October 7th, 2005 at 05:11 AM. Reason: typo
    Har Har

  3. #3
    Join Date
    Apr 2005
    Posts
    1,828

    Re: Stop an object from being created

    What kind of variable this instance is? is it of integer type?

  4. #4
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up Re: Stop an object from being created

    What kind of variable this instance is? is it of integer type?
    Probably, yes. You might also want to take a look at this FAQ - What is a Singleton class? In the codeguru C++ articles, there was an article by Darwen on template implementation of the Singleton base classes. You might want to search for it and have a look as well.
    Quote Originally Posted by PadexArt
    if you plan to use something similar in a multithreaded environment, make sure you add the appropriate safety mechanisms.
    PadexArt, can we not use this in multithreaded environments? I had some time earlier read an article by Danny Kalev and he wrote that the usage of "new" operator makes it suitable for multi-threaded environments as well since the operator new is thread safe. This is his article that I am referring to - Implementing the Singleton Design Pattern. Regards.

  5. #5
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496

    Re: Stop an object from being created

    Quote Originally Posted by exterminator
    PadexArt, can we not use this in multithreaded environments?
    Yes, we can but the ++instances and --instances should be made thread safe.
    Har Har

  6. #6
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up Re: Stop an object from being created

    Quote Originally Posted by PadexArt
    Yes, we can but the ++instances and --instances should be made thread safe.
    Ok..got you. Thanks. I have got one more query in this regard. What do people mean when they say atomic creation of objects? I don't understand this term, tried finding out but seems like its an informal name given to something that I dont know. Could you please expand on this? Regards.

  7. #7
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496

    Re: Stop an object from being created

    Quote Originally Posted by exterminator
    Ok..got you. Thanks. I have got one more query in this regard. What do people mean when they say atomic creation of objects? I don't understand this term, tried finding out but seems like its an informal name given to something that I dont know. Could you please expand on this? Regards.
    I can only think to 2 things:
    1. have the object created and initialized with the same call ( see the CFile exemple)
    2. a "transaction" like behavior
    - lock object for MT acces
    - create object
    - initialize object
    - should errors occur at any point, throw exception/return code and clean up
    - unlock object for MT acces

    with 2, you can see the objects creation and initialization as a single atomic instruction even if more are actually invoked.
    Har Har

  8. #8
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Stop an object from being created

    Quote Originally Posted by PadexArt
    Yes, we can but the ++instances and --instances should be made thread safe.
    Which can be done with InterlockedIncrement and InterlockedDecrement or the other InterlockedXX functions
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  9. #9
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496

    Re: Stop an object from being created

    Quote Originally Posted by Marc G
    Which can be done with InterlockedIncrement and InterlockedDecrement or the other InterlockedXX functions
    yes. but I was trying to leave the OP the pleasure of discovering those.
    Har Har

  10. #10
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Stop an object from being created

    Quote Originally Posted by PadexArt
    yes. but I was trying to leave the OP the pleasure of discovering those.
    Ah, sorry to have screwed the fun then
    Anyway, I replied it because it's not always obvious and when I needed to make ++ and -- threadsafe, I didn't immediately found those nifty functions
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured