A shared_ptr
is a sort of reference. A pointer with memory management. You can store the plain shared_ptr
since the internal refers to the same data anyway(Copy constructor increment reference count, etc).
Using shared pointers in map
-
02-10-2022 - |
Pergunta
I'm trying to decide what is best choice to use in my HW. I have a map (I coded it) that supposed to store integer id's as keys and shared pointer of class named fan as values:
Map<Id, shared_ptr<Fan>> Online_list;
what is better to use shared_ptr<Fan>&
or none reference ?
My homework is about creating server like Facebook with fans to be on-line and offline,so im having two maps one called Online_list
and other is Offline_list
, so when fan is disconnected i need to remove him from on-line list and add him to offline list.
Solução
Outras dicas
Usually it's best to not store a pointer at all, but just store the Fan
object by-value. Does it really make sense for two
things to own this Fan
object?
However, assuming that your design is correct, then you should simply store the shared_ptr
by-value.