private:
string mySender;
string temp;
mySender and temp declared as string. I still couldnt figure out whats wrong with my following code ??
Code:string Message::getMessage() const {
temp = mySender;
return mySender;
}
Printable View
private:
string mySender;
string temp;
mySender and temp declared as string. I still couldnt figure out whats wrong with my following code ??
Code:string Message::getMessage() const {
temp = mySender;
return mySender;
}
If temp is a member variable, your getMessage function changes the state of the object, thus it can't be const.
This should compile, although you might want to reconsider the logic of your class. I would consider a non-const function prefaced with "get" somewhat misleading.
Code:string Message::getMessage() {
temp = mySender;
return mySender;
}
Well, you can modify member data in a const function if the member data is declared as mutable. This, however, should be reserved for very special cases when the function is logically const (logically does not change the object), but may need to set a flag (for example, counting how many times a certain piece of data has been accessed).
And, for future reference, it helps if you post what errors/problems you get along with the code.