Assuming this is about C++, putting a dumb pointer into a struct
is indeed dangerous if this pointer refers to an object (or an array of objects) that is owned by the struct
's instance (that is, the instance is responsible for the proper deletion of the object).
Copying an instance of the struct
means copying all of its members. After that, you end up with several instances of that struct
having pointers to the same object. When all of these instances are about to be deleted, you would have to delete the object referred to by the pointer. However, if one of them is about to be deleted, it is often hard to tell if other instances are still around somewhere.
A way out of this would be reference-counting, done by clever implementing constructors, destructors, and assignment. Fortunately you don't need to implement this yourself, since it's already been done in so-called smart pointer. Either your standard library ships TR1's std::tr1::shared_ptr
or it already comes with C++11's std::shared_ptr
or you can download the boost libraries and use their boost::shared_ptr
.