I have a doubt about how to keep a class... probably it is extremely simple to solve
This is the story:
- A class (A) has to keep a copy of other class (B)
- A can keep:
-- Case 1. a pointer to an instance of B created with new() by A
-- Case 2. a pointer to an instance of B handled in other place
- When A is destroyed
-- Case 1. B is destroyed in A destructor with a delete
-- Case 2. B is not destroyed. The owner has to destroy it
The problem of Case 2 it is that it is possible that B owner destroys B before A is destroyed... so if A is used in between, it can cause problems.
I have a doubt about how to keep a class... probably it is extremely simple to solve
This is the story:
- A class (A) has to keep a copy of other class (B)
- A can keep:
-- Case 1. a pointer to an instance of B created with new() by A
-- Case 2. a pointer to an instance of B handled in other place
- When A is destroyed
-- Case 1. B is destroyed in A destructor with a delete
-- Case 2. B is not destroyed. The owner has to destroy it
The problem of Case 2 it is that it is possible that B owner destroys B before A is destroyed... so if A is used in between, it can cause problems.
What is the proper way to do it?
It would be more helpful to explain the situation with the code. That said, if the task is to "A class (A) has to keep a copy of other class (B)", then what is wrong with