What you want to be doing is this:
class teacher {
public:
char*& operator[](int i);
protected:
char** contents;
};
char*& teacher::operator[](int i) {
return contents[i];
}
Here you have an array of char*
, i. e. C-style strings. The []
operator returns one of these char*
as a reference, so that the code can change the pointer to point to some other string. This is according to the common C++ accessor idiom.
If you would choose to use std::string
, you would just need to replace all occurences of char*
with std::string
.
Personally, however, I don't say that this approach is necessarily the best one; I have some objections against interfaces that expose the inner workings of a class like this. In this case any caller can obtain a reference to a pointer used by the class, and can use it at any later time to directly modify the contents of teacher
without going through another teacher
method. This makes it impossible to ensure any consistency of contents
with the value of any other data member of teacher
.
I try to avoid accessors in code I write, and favour methods that actually do something. That helps avoid code clutter and allows method names to name higher level operations. But that is just my opinion, many other people see it very differently.