I have one instance of Class A. It has two members, Object A and Object B. I need to create ONE instance of Object A (which is quite a large object), and TWO instances of object B, and then give both instances of object B access to that object A. They don’t ever need to change object A, they just need to access its info.
Right now, I got it all to work by creating Object A as a pointer, passing it into both instances of Object B, who have their own pointer that then points to the same place in memory as Object A’s pointer. This works fine, but I think that’s a pretty big No-No right? Because once Class A deletes the original pointer I’ll have some dangling pointers, right? Is there a better way to do this?
(FYI – Object A takes a couple seconds to load at a time in the program where I need a fast load time, that’s why I have to create only one instance and pass it to both of Object Bs instead of letting Object B create their own instances of Object A. )
If I follow you correctly, you want something like this:
Using no pointers, no allocations, so no dangling pointers/references or memory leaks. When
Agets destroyed so doesbar1_andbar2_too along withfoo_, so the two instances ofBarwill not be left with any dangling references either.