Original Post
The problem: lets say I have 2 classes. Class-one and class-two. Class-one _dynamically_ allocates class-two. Class-one gets initialised on the stack (class-two then, lets say, gets allocated in the constructor...dynamically). Class-one's instance is passed (copied!) onto a list which keeps track of all class-one objects. Now we have 2 instances of class-one: class-one on the stack (visible to the user (programmer)) and class-one on the list (transparent to the user). Both instances have a variable which points to the same class-two instance! Class-two will thus be deleted twice, which causes a segment error. Is there a widely-used solution / work-around / code layout which would solve this problem? Should I plainly write a function called 'reallocate_dynamic_data()'? What if class-two has a dynamic instance of class-three? Wouldn't this mean a lot of reallocating in the end? Performance? What do you do to avoid this? Thanks.