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?

4
  • 4
    If you want to reuse B, don't delete it. Commented Dec 14, 2013 at 0: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. Commented Dec 14, 2013 at 1: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. Commented Dec 14, 2013 at 1:09
  • @polkadotcadaver: yes, I think I must have misread the question. It makes sense now. Commented Dec 14, 2013 at 1:12

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

6 Comments

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.
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.
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.
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).
@mycroft.holmes Edited to include the suggestion in my comment.
|
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).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.