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.
- the raytracer becomes responsible of deallocating memory allocated sphere object
- the user needs deallocate memory allocated sphere object.
- 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
Post a Comment