The pointer is not an object, so it does not matter if the pointer is const when it is the return value. Consider this:
int x; int f() { return x; } const int g() { return x; }
There is really no difference between f () and g ().
But that can make a difference if you replace all "int" with some other class name. "const" may prevent you from getting some error, for example. when you write the copy constructor incorrectly.
class T { int *x; public: T() {x = new int;} void set_x(int n) {*x = n;} int get_x() const {return *x;} }; T a; T f() { return a; } const T g() { return a; } int main() { f().set_x(123);
So, if you want to prevent changing the data of the object, referring to the return value, you need:
class T { public: const T* func() const { return this; } };
Rnmss
source share