Consider the following class
class Person
{
public:
explicit Person(RegistrationNumber id, std::string const& name);
RegistrationId id() const;
std::string const& name() const;
Person& name(std::string const&);
// ...
private:
RegistrationNumber m_id;
std::string m_name;
// ...
};
Now I want to be able to find a person by its id, and update its name. For that, I could use
std::set<Person, PersonIdCompare>
, but then I cannot modify the record without aconst_cast
.Is it better then to store Persons in a
std::map<RegistrationId, Person>
, so the record can be changed, though this requires the id to be stored twice.Or maybe I should use a map, and remove the id from the
Person
class. But then I cannot get the id if I only have access to thePerson
, and notstd::pair<RegistrationNumber const, Person>
, so this may require to pass these pairs around.
What option is most efficient or less error prone. Think of the scenario where someone adds a setter for the person id, which would break option 1 and 2, or storing the id twice as for option number 2.