0

I suspect it is not possible, but in the situation below having created A and B I'd like to reuse B (by placing it into a stack ready for reuse) but delete A. They are two of many classes derived from the parent class and I'd like the code responsible for deleting objects to be able to behave the same for all derived objects.

Is it possible to override Derived2's destructor/delete operator such that data2's destructor isn't called? Then it would be a simple matter to put B into the stack with all of the data allocated during its construction ready for reuse.

(EDIT: For clarification, I want to reuse B which has a large number of member variables of different types depending on which derived class is involved. I do appreciated all suggestions but I used a very simple example to find out if this particular approach was possible and because the actual classes contain a huge amount of extra code irrelevant to the question at hand)

class Parent{
    ...
};

class Derived1:Parent{
    DataClass data1;
};

class Derived2:Parent{
    DataClass data2;
};

Derived1* A = new Derived1();
Derived2* B = new Derived2();
delete A;
delete B;

Would doing that (if it were possible) be considered a breach of the RAII design pattern?

curiousguy
  • 8,038
  • 2
  • 40
  • 58
mycroft.holmes
  • 177
  • 1
  • 1
  • 6
  • 4
    If you want to reuse B, don't delete it. – Barmar Dec 14 '13 at 00:49
  • No, I want to reuse B but I want the responsibility for its reuse to be encapsulated in Derived2 and not have to increase the complexity/dependency of the code responsible for deleting objects. Apologies if my description of the problem was vague. – mycroft.holmes Dec 14 '13 at 01:01
  • @SteveJessop I don't think he wants to, I think he wants to ensure that the object is either on his stack OR live elsewhere in the code, exclusively and safely. –  Dec 14 '13 at 01:09
  • @polkadotcadaver: yes, I think I must have misread the question. It makes sense now. – Steve Jessop Dec 14 '13 at 01:12

2 Answers2

1

*SNIP - After clarification of the scenario, I posted this answer in a comment originally *

How about this. In your stack, store unique_ptrs. When you pop from the stack, you grab the raw pointer from the unique_ptr and shove it into another unique_ptr - this second one is declared with a custom deleter which, on destruction, retains the location of the stack and performs the reverse of the pop operation. This will work with shared_ptr too but obviously it depends on what you need.

It's independent of the type of B, and could be encapsulated entirely in the stack class (barring the unique_ptr+deleter type, but that would just be a typedef).

  • Thanks for the suggestion. I'm familiar with smart pointers but can't think of an unobtrusive way they can solve this problem. I want the object to be popped from the stack when it's in use and pushed back when it's deleted. As far as I can see I'd have to keep a copy of it in the stack (or somewhere else) to prevent its deletion using a shared pointer, though I certainly could be missing a simpler/better solution. – mycroft.holmes Dec 14 '13 at 00:58
  • How about this. In your stack, store unique_ptrs. When you pop from the stack, you grab the raw pointer from the unique_ptr and shove it into another unique_ptr - this second one is declared with a custom deleter which, on destruction, retains the location of the stack and performs the reverse of the pop operation. This will work with shareD_ptr too but obviously it depends on what you need. –  Dec 14 '13 at 01:05
  • That sounds like it would work and without modifying the existing code. More complex than I'd hoped but I wasn't very optimistic about my approach being feasible. – mycroft.holmes Dec 14 '13 at 01:12
  • Like you say, it's independent of the type of B, and could be encapsulated entirely in the stack class (barring the unique_ptr+deleter type, but that would just be a typedef). –  Dec 14 '13 at 01:13
  • @mycroft.holmes Edited to include the suggestion in my comment. –  Dec 14 '13 at 01:16
  • Marked as correct since it's the best solution to the problem as described. – mycroft.holmes Dec 14 '13 at 13:49
1

Instead of storing data2 by value, store it using a pointer and allocate it in the constructor. That way it won't get deleted when you delete the B (unless you call delete inside Derived2 destructor).

Paweł Stawarz
  • 3,952
  • 2
  • 17
  • 26