开发者

Returning const 'this' pointer

开发者 https://www.devze.com 2023-02-01 00:11 出处:网络
if this is a const pointer to class\'s object how can you return a const pointer from non-const return type?

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

开发者_如何学运维Class T
{
 public:
   T* func(){return this;}
};


Firstly, this is not a "const pointer". Where did you get that strange idea? Pointer this has scalar type and is not an lvalue, meaning that it can't possibly be "const" or "non-const". Pointer this is an rvalue and it is non-modifiable.

Secondly, the code in your question is valid regardless of whether the pointer involved is const or not. For example, the following code is valid for exactly 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 pointer value. It does not matter whether p is const or not - making a copy of const value does not in any way violate its constness.

This is exactly what's done in your code sample - you are returning a copy of this's value. Why would you even care whether this is const or not?


Unless the member function is const-qualified (e.g. T* func() const instead of just T* func()), this is of type T*.

this is not itself modifiable (so you can't assign something to this), but it's just a pointer to an object of type T like any other and can be used as such.


AndreyT provided the explanation and here is the reference.

From standard docs 9.3.2.1 The this pointer,

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

which explains the question that you asked and the comment you made to AndreyT.

Hope this one also helps.


A pointer is not a object, so it does not matter if the pointer is const when it is a return value. Consider this:

int x;
int f() {
    return x;
}
const int g() {
    return x;
}

There's actually no difference between f() and g().

But it may make difference if you replace all "int" to some other name of a class. "const" may prevent you from some error, e.g. when you don't write a copying constructor properly.

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 modifying data of the object by referring to the return value, what you need is:

class T
{
 public:
   const T* func() const
   { return this; }
};
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号