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?
Printable View
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?
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.
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:
What kind of variable this instance is? is it of integer type?
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.Quote:
Originally Posted by PadexArt
Yes, we can but the ++instances and --instances should be made thread safe.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.Quote:
Originally Posted by PadexArt
I can only think to 2 things:Quote:
Originally Posted by exterminator
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.
Which can be done with InterlockedIncrement and InterlockedDecrement or the other InterlockedXX functions ;)Quote:
Originally Posted by PadexArt
yes. but I was trying to leave the OP the pleasure of discovering those. :DQuote:
Originally Posted by Marc G
Ah, sorry to have screwed the fun then :pQuote:
Originally Posted by PadexArt
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 :wave: