|
-
October 17th, 2010, 01:50 PM
#1
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.
-
October 17th, 2010, 02:03 PM
#2
Re: Question on *this and const
 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.
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).
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
October 17th, 2010, 04:21 PM
#3
Re: Question on *this and const
 Originally Posted by D_Drmmr
 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.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
October 17th, 2010, 04:34 PM
#4
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.
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
-
October 17th, 2010, 05:44 PM
#5
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->?
-
October 17th, 2010, 10:22 PM
#6
Re: Question on *this and const
 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).
-
October 17th, 2010, 10:52 PM
#7
Re: Question on *this and const
 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
-
October 18th, 2010, 01:52 AM
#8
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:
 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*.
 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*.
Last edited by monarch_dodra; October 18th, 2010 at 01:56 AM.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|