Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can 'this' be assigned a pointer to it?

62 views
Skip to first unread message

fl

unread,
Mar 11, 2015, 1:30:24 AM3/11/15
to
Hi,

I read C++ for some time, but I still has problem in understand it.
Below is from web tutorial. It requires to debug it, but I cannot figure out
what is wrong with the void change function.

The error message says that this must be a lvalue. Does that mean 'this'
cannot be assigned a value? Then the change function is invalid?
What it wants me to do?

Please at least give a little hint on it.


thanks,






#include<iostream>
using namespace std;

class Test
{
private:
int x;
public:
Test(int x = 0) { this->x = x; }
void change(Test *t) { this = t; }
void print() { cout << "x = " << x << endl; }
};

int main()
{
Test obj(5);
Test *ptr = new Test (10);
obj.change(ptr);
obj.print();
return 0;
}

Norman J. Goldstein

unread,
Mar 11, 2015, 1:59:37 AM3/11/15
to
There are a couple of things, here, that are confusing:
1. Overusing the variable name, 'x'. Change the declaration
of Test to be Test( int x0 = 0 );
2. The rules don't allow you to assign to "this". However, you
can pass around &obj and ptr inside the main function to change
which Test object you are working with.

Robert Wessel

unread,
Mar 11, 2015, 2:00:47 AM3/11/15
to
On Tue, 10 Mar 2015 22:29:48 -0700 (PDT), fl <rxj...@gmail.com>
wrote:
"9.3.2 The this pointer
In the body of a nonstatic (9.3) member function, the keyword this is
a nonlvalue expression whose value is the address of the object for
which the function is called."

this is not an lvalue, IOW, you can't modify it.

JiiPee

unread,
Mar 11, 2015, 3:24:56 AM3/11/15
to
a good answer, +1 :)

Lőrinczy Zsigmond

unread,
Mar 11, 2015, 5:17:35 AM3/11/15
to
This code wouldn't do any good, even if worked:
void change(Test *t) { this = t; }

But this version would do something mode-or-less useful:
void change(Test *t) { *this = *t; }

Martijn Lievaart

unread,
Mar 11, 2015, 5:50:24 AM3/11/15
to
On Tue, 10 Mar 2015 22:29:48 -0700, fl wrote:

> Hi,
>
> I read C++ for some time, but I still has problem in understand it.
> Below is from web tutorial. It requires to debug it, but I cannot figure
> out what is wrong with the void change function.
>

Others pointed out the problem itself, just wanted to point out that a
tutorial that makes such basic errors should be ditched immediately.

M4

Melzzzzz

unread,
Mar 11, 2015, 11:20:23 AM3/11/15
to
`this` is just syntactic sugar for first parameter of function.
say func(Test* this) ....
Even if you assign value to it nothing will change.

Haddock

unread,
Mar 11, 2015, 12:39:46 PM3/11/15
to
Assigning a different reference to the this pointer is like, say, assigning 5 to 3. Then everywhere in your system every 5 is now a 3. This is something that shouldn't be done. Needless to say that the application would also shut down immediately if every 5 would be a 3 from now on. I don't know whether this sample is meaningful, but I think you know what I mean.

Greg Martin

unread,
Mar 11, 2015, 12:49:12 PM3/11/15
to
The tutorial was suggesting the user should debug the program so it
would make sense there should be an error for the reader to fix.

--
http://www.softsprocket.com

Martijn Lievaart

unread,
Mar 12, 2015, 10:20:31 AM3/12/15
to
On Wed, 11 Mar 2015 16:48:51 +0000, Greg Martin wrote:

> The tutorial was suggesting the user should debug the program so it
> would make sense there should be an error for the reader to fix.

Fair enough, might have the subgoal of reiterating that this is const.

M4

Öö Tiib

unread,
Mar 12, 2015, 2:21:14 PM3/12/15
to
The keyword 'this' is actually a reference with pointer semantics (it can't
be modified and it can't be 'nullptr'). The sole reason why it is not
a keyword returning reference was that the C++ language did not have
references when the 'this' keyword was added.
0 new messages