Return pointer const 'this'

if this is a const pointer to a class object, how can you return a const pointer from a non-constant return type?

 Class T { public: T* func(){return this;} }; 
+8
c ++
source share
4 answers

Firstly, this not a "constant pointer". Where did you get such a strange idea? The this pointer is a scalar type and is not an lvalue value, which means that it cannot be "const" or "non-const". The this pointer is the value of r, and it is not modified.

Secondly, the code in your question is valid regardless of whether the pointer is included or not. For example, the following code is valid for the same reason

 int *const p = 0; /* a const pointer */ int *foo() { return p; /* OK, no error here */ } 

What is returned in this case is a completely independent copy of the original value of the pointer. It does not matter whether p const or not - creating a copy of the value of const in no way violates its constness.

This is exactly what was done in your code example - you are returning a copy of the this value. Why don't you care whether this const will be or not?

+9
source share

If the member function is not const-qualified (for example, T* func() const instead of just T* func() ), this is of type T* .

this by itself is not modified (therefore you cannot assign something to this ), but it is just a pointer to an object of type T , like any other, and can be used as such.

+2
source share

AndreyT provided an explanation, and here is the link.

From standard documents 9.3.2.1 This pointer

In the body of a non-static (9.3) member function, the this keyword is an rvalue expression whose value is the address of the object for which the function is being called. The type of this in a member function of class X is X *. If the member function is declared const, the type of it is const X *, if the member function is declared mutable, the type is volatile X *, and if the member function is declared const volatile, the type is constant volatile X *.

which explains the question you asked and the comment you made for AndreyT.

Hope this helps too.

+1
source share

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); // The value is modified // even though a return value is not a l-value. g().set_x(123); // Compile Error } 

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; } }; 
0
source share

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


All Articles