Guest User

Untitled

a guest
Jun 24th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. class MyClass
  2. {
  3. private:
  4. std::auto_ptr<MyOtherClass> obj;
  5.  
  6. public:
  7. MyClass()
  8. {
  9. obj = auto_ptr<MyOtherClass>(new MyOtherClass());
  10. }
  11.  
  12. void reassignMyOtherClass()
  13. {
  14. // ... do funny stuff
  15. MyOtherClass new_other_class = new MyOtherClass();
  16. // Here, I want to:
  17. // 1) Delete the pointer object inside 'obj'
  18. // 2) Re-assign the pointer object of 'obj' to 'new_other_class'
  19. // so that 'obj' now manages 'new_other_class' instead of the
  20. // object that just got deleted manually
  21. }
  22. };
  23.  
  24. void MyClass::reassignMyOtherClass()
  25. {
  26. // ... still, do more funny stuff (flashback humor :-)
  27. MyOtherClass new_other_class = new MyOtherClass();
  28. obj.reset(new_other_class);
  29. }
  30.  
  31. obj.reset( new MyOtherClass() );
  32.  
  33. MyClass():
  34. obj( new MyOtherClass() )
  35. {
  36. }
Add Comment
Please, Sign In to add comment