dimanche 6 janvier 2019

Should I assume weak pointer usage if there is any cyclic relationship?

Say I've a Person class, with name, age, and bestFriend as properties. I'm confused about how to best represent bestFriend. Multiple Person instances can be pointing to the same bestFriend, which rules out unique_ptr. And there is also a possibility of a cyclic relationship, which rules out shared_ptr. So, is using weak_ptr ideal in this case?

class Person {
    std::string name;
    int age;
    std::weak_ptr<Person> bestFriend;
};

Aucun commentaire:

Enregistrer un commentaire