If you have a class customer (for instance) which has a container std::vector<std::string> that stores customer's nicknames and you want to access it from other classes so that you can get the first nickname in the list, x-th name in the list and you want to be able to add nicknames to the list - how would you implement it?
Perhaps you could write something like this:
Code:
class customer {
public:
typedef std::vector<std::string>::size_type size_type;
void add_nickname(const std::string& nickname)
{
m_nicknames.push_back(nickname);
}
std::string get_nickname(size_type n) const
{
return m_nicknames[n];
}
size_type num_nicknames() const
{
return m_nicknames.size();
}
private:
std::vector <std::string> m_nicknames;
};
After all, if you want to know what is the first nickname, you can just call customer_obj.get_nickname(0).
Maybe using boost:: optional would be better for get_nickname functions (because list could be empty) ?
You could simply require the caller to check that the customer has nicknames before trying to access the nicknames. This could be enforced by returning m_nicknames.at(n) instead so as to throw an out_of_range exception if necessary.
What do you think about those long function names? On one side it is sensible so you can be sure what function does, on other side it doesnt 'look good'..
The "customer" seems unnecessary since this is with respect to a class named customer.