David Pashley: Return of Accessors vs. public member variables
Steinar,
Imagine a Word class:
class WordA
public:
char * word;
;
class WordB
private:
char * word;
public:
void setWord(char * w) word = w;
char * getWord() return word;
;
WordA worda;
worda.word = "WordA";
WordB wordb;
wordb.setWord("WordB");
printf("%s %s", worda.word, wordb.getWord());
Fine, this works well. Now imagine that you want to change word from a
char * to a std::string to stop you dealing with
pointers. For the WordBclass you only need to change the
getWord function. You don't need to change any of your class's
users. For the WordA class, you have a problem because you can't
automatically convert from a std::string to a char *,
so all your users have to change from object.word to
object.word.c_str().
class WordA
public:
std::string word;
;
class WordB
private:
std::string word;
public:
void setWord(char * w) word = w;
char * getWord() return word.c_str();
;
WordA worda;
worda.word = "WordA";
WordB wordb;
wordb.setWord("WordB");
printf("%s %s", worda.word.c_str(), wordb.getWord());
This is the punishment you get for exposing the internal details of
your class to your users. Please ignore any specific mistakes in my
examples; the principles work the same with different types.
I won. I completed 50,000 words in time. Of course, only about 13,000 words are directly usable if and when I end up editing the story, the rest will have to be removed – they were imposed by the strict timetable and the explicit leave to lower one’s standards.
But, bottom line:
I WON!
Ps. Please don’t ask to see the manuscript. It’s not that good