I would like my c++ code to be as encapsulated as i can is this manner of return the iterators is ok?
const map<string,bool>::iterator getFollowers() {
return followers.begin();
}
const map<string,bool>::iterator getFollowing() {
return following.begin();
}
the full code:
#ifndef twitClient_twitUser_h
#define twitClient_twitUser_h
#include <map>
#include <iostream>
#include <string>
using namespace std;
class user {
string username;
map<string,bool> followers;
map<string,bool> following;
string name;
public:
user(string username):username(username) {
followers [username] = false;
following [username] = false;
}
bool removeFollower (string friendName);
bool addFollower(string friendName);
bool stopFollowing(string friendName);
bool startFollowing(string friendName);
const map<string,bool>::iterator getFollowers() {
return followers.begin();
}
const map<string,bool>::iterator getFollowing() {
return following.begin();
}
};
There’s nothong wrong with your approach, except that you may want to add const access methods too, foe example
Plus you want to add access to the
end()iterators too.Concerning encapsulation, you encapsulate the map, but your clients are exposed to
map<string, bool>::iterator. There’s a very interesting article about hiding those dependencies here. It is by no means trivial, but it is still worth considering.