4

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
  }
}
Nezquick
  • 455
  • 4
  • 11

2 Answers2

4

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; ...

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
0

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.

djechlin
  • 59,258
  • 35
  • 162
  • 290