Reputation: 475
If I override the destructor of my class, is it possible to made some check in it if that instance should be killed or sent to a pool? I want to make simple to reuse the obj, the user only needs to free it (or leave for compiler) and the destructor would check if that's reusable.
ReusableClass::~ReusableClass() {
if (x == 1) {
// abort destructor, send to pool
}
}
Upvotes: 4
Views: 120
Reputation: 60788
Of course not - you would have no reference to the object either - how could you recycle the object then?
So solve that problem and you'll have the solution. Use a smart pointer and store the reference wherever you actually need it, perhaps in a map or owned by another object (this is now looking more like a Java solution, for instance), or manually delete it (this is less smart) when ready.
Upvotes: 0
Reputation: 153955
Once the destructor of an object started running the object is considered dead: 12.4 [class.dtor] paragraph 14:
Once a destructor is invoked for an object, the object no longer exists; ...
Upvotes: 4