c++ - Who should own the pointer -


while programming, many times i've come across following design choice: user creates object , passes other object processes in way in second stage.

as example, imagine raytracer. user creates sphere properties , calls raytracer.addtraceable(sphere). now, there 3 ways can think of doing this.

  1. the raytracer becomes responsible of deallocating memory allocated sphere object
  2. the user needs deallocate memory allocated sphere object.
  3. the raytracer copies sphere object , both user , raytracer deallocate local copy.

generally best design choice in such case? there other options besides ones mentioned (not including smart pointers)?

ps: i've come across same problem in plain c when using object oriented approach.

the consistent use of raii makes moot point. using smart pointer such std::shared_ptr object owned all of pointers , deleted after last pointer destroyed.

c doesn't have convenient way express raii idiom.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -