user2991252
user2991252

Reputation: 788

Return string-pointer from member-function

I dont understand why I get an error message when I return a char array from an accessor.

in the classdefinition

public:
   const char *getString() {
     return _strPtr;
   }
private:
   char* _strPtr;
   int _strLen;

then I am trying to access the pointer in the class

_strLen = strlen(String.getString());

But I get the error message: candidates are const char* getString();

I cannot see what I am doin wrong right now

Thanks in advance!!!

String String::operator=(const String& string) {

    // code ..... 

   _strLen = strlen(string.getString());

   //code ...
}

Upvotes: 0

Views: 487

Answers (1)

juanchopanza
juanchopanza

Reputation: 227400

The argument to the operator is const reference, so the invoked method needs to be const:

const char* getString() const { .... }
                        ^^^^^

Upvotes: 3

Related Questions