C2556: overloaded function differs only by return type

I read Effective C ++, which tells me that "member functions that differ only in their constant can be overloaded."

Example book:

class TextBlock { public: const char& operator[](std::size_t position) const; char& operator[](std::size_t position); private: std::string text; } 

In my example below, the saved pointer is used.

 class A { public: A(int* val) : val_(val) {} int* get_message() { return val_; } const int* get_message() { return val_; } const; private: int* val_; }; 

I get:

error C2556: 'const int * A :: get_message (void)': the overloaded function differs only in the return type from 'int * A :: get_message (void)'

What is the difference? Is there a way I can fix the class, so I have a constant and non-constant version of get_message?

+4
source share
1 answer

You put the const qualifier for your get_message() function in the wrong place:

 const int* get_message() const { return val_; } // ^^^^^ // Here is where it should be 
+15
source

Source: https://habr.com/ru/post/1484467/


All Articles