Question on *this and const
Hey guys i'm new to the forum and c++ in general, and sorry if i'm posting in the wrong place but i'm a little hung up on question that was handed out a review session here is the question verbatim:
The expression *this refers to the object for which a member function is invoked. What is the type of this in a non-const member function of class X?
a) const X*
b) const X* const
c) X *const
d) X*
The answer was c), I thought I knew what the question was asking except that I thought c) and d) were both right answers. After I saw the correct answer I have no idea what the question means anymore, could someone explain why c) is right and d) is not, and the train of thought required to understand this question.
Re: Question on *this and const
Quote:
Originally Posted by
RK883
The expression *this refers to the object for which a member function is invoked. What is the type of this in a non-const member function of class X?
a) const X*
b) const X* const
c) X *const
d) X*
The right answer is e) none of the above. As the this pointer is dereferenced, the type of the expression is a reference to X, not a pointer.
Quote:
The answer was c), I thought I knew what the question was asking except that I thought c) and d) were both right answers. After I saw the correct answer I have no idea what the question means anymore, could someone explain why c) is right and d) is not, and the train of thought required to understand this question.
The point is that you cannot assign to this. I remember a discussion here a while ago about whether this is actually a const pointer (i.e. not pointer-to-const) or not, but I don't remember the answer. Doesn't really matter anyhow, as long as you remember that you cannot assign to this... Which is exactly why these type of questions are bad: they focus on testing declarative knowledge (learning by heart) rather than procedural knowledge (learning how to use).
Re: Question on *this and const
Quote:
Originally Posted by
D_Drmmr
Quote:
Originally Posted by
RK883
The expression *this refers to the object for which a member function is invoked. What is the type of this in a non-const member function of class X?
The right answer is e) none of the above.
Actual, the question really is the type of "this", and not "*this", so either c or d is (potentially) correct.
I'm not sure if you can or can't assign to this. It depends if "this" is passed by value or reference. My guess is that the implementation passes this by value, since it is a pointer, so you can probably assign to it, but the change will be "lost" at the end of the member method. However, the standard could have made this const, just for safety, and to protect you from yourself.
My answer would be d, but I'd have to read the standard to be sure. It is a bit late, I'll do it tomorrow.
Re: Question on *this and const
To programmers this is a constant pointer if *this is non-const and a constant pointer to a constant *this if *this is const. However to a compiler this is mutable. It has to be to support multiple inheritance.
Re: Question on *this and const
So can "this" have its type changed as the question suggests? I thought "this" was always a const reference to the object, additionally is *this the same as this->?
Re: Question on *this and const
Quote:
Originally Posted by
RK883
So can "this" have its type changed as the question suggests? I thought "this" was always a const reference to the object, additionally is *this the same as this->?
If it does not make sense to say "this++", which it does not, then this may be assumed to be a const pointer, eg, it cannot be modified. This is what (c) has over (d).
If it were a const member function rather than a non-const member function, then the answer would be (b).
Re: Question on *this and const
Quote:
Originally Posted by
Lindley
If it does not make sense to say "this++", which it does not, then this may be assumed to be a const pointer, eg, it cannot be modified. This is what (c) has over (d).
If it were a const member function rather than a non-const member function, then the answer would be (b).
I see know,your explanation cleared it up very nicely for me, thank you
Re: Question on *this and const
It is actually a bit more sutle than that, and also a bit different between classic C++98/03 and C++0x:
Quote:
Originally Posted by C++0x draft 3126 Section 9.3.2
In the body of a non-static (9.3) member function, the keyword this is a prvalue 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*.
Quote:
Originally Posted by C++ 1996 draft Section 9.3.2
In the body of a nonstatic member function, the keyword this is a non-lvalue 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*.
What this basically means is that "this" is more than just a const, it is pretty much the same as literal. It is not const, but it is illegal to modify its value, just as it is not possible to change the value "true", "false" or "nullptr".
So to sum up, for all intents and purposes, "this" behaves like c (X* const), but from a strict standard point of view, "this" is a prvalue (or non-lvalue in C++98/03), of type X*.