assign to the 'this' pointer.

  • Follow


Hello,
Is it OK to assign to the 'this' pointer?
If the object was created using 'placement new' can I change 'this' to point
to different objects at will?
If not why not?
Thanks
Todd.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply toddmarshall2002 3/3/2004 8:42:46 PM

toddmarshall2002@yahoo.com wrote:
> Hello,
> Is it OK to assign to the 'this' pointer?

No; that was banned nearly 15 years ago.

> If the object was created using 'placement new' can I change 'this' to point
> to different objects at will?
> If not why not?

Placement new lets you decide where an object is created, but it is
not possible to change that decision within the constructor by
assigning to 'this'.

The rules that prevent changing 'this' are that 'this' is an rvalue of
pointer type, and that you cannot assign to an rvalue of built-in
type.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Ben 3/4/2004 1:09:52 PM


<toddmarshall2002@yahoo.com> wrote in message
news:c7aadd5c.0403030826.7b6c1b6a@posting.google.com...
> Hello,
> Is it OK to assign to the 'this' pointer?
> If the object was created using 'placement new' can I change 'this' to
point
> to different objects at will?
> If not why not?

One very important property of an object is its identity. It is unique for
each object and, of course, never changes in the lifetime of the object. In
C++, the identity of an object is the "this" pointer. It cannot be changed
while the object exists.

An object is not moved in memory. Instead, a new one is created and the old
one is destroyed.

-- 
Fr�d�ric Lachasse - ECP86


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Fr 3/4/2004 2:09:39 PM

Hi,

toddmarshall2002@yahoo.com wrote:

> Is it OK to assign to the 'this' pointer?

No.

> If not why not?

Standard says so.
9.3.2/1: "... the keyword this is a non-lvalue expression..."

You cannot assign to the non-lvalue value.
In order to better understand this (pun intended) you can imagine that 
"this" is not a variable, but a function call "getThis()" that return 
the address of the "current" object.
Now:

this->i = 7;

behaves like

getThis()->i = 7;

which is OK.
But here:

this = NULL; // error

it will not work, just like the following:

getThis() = NULL; // error

-- 
Maciej Sobczak : http://www.msobczak.com/
Programming    : http://www.msobczak.com/prog/


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Maciej 3/4/2004 10:03:39 PM

Ben Hutchings <do-not-spam-benh@bwsint.com> wrote in message
news:<slrnc4cnuf.okv.do-not-spam-benh@shadbolt.i.decadentplace.org.uk>...
> toddmarshall2002@yahoo.com wrote:

> > Is it OK to assign to the 'this' pointer?

> No; that was banned nearly 15 years ago.

Even then, it was only allowed in the constructor, and was associated
with very particular semantics.

--
James Kanze           GABI Software        mailto:kanze@gabi-soft.fr
Conseils en informatique orient�e objet/     http://www.gabi-soft.fr
                    Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply kanze 3/5/2004 10:39:36 AM

4 Replies
413 Views

(page loaded in 0.075 seconds)

Similiar Articles:













7/21/2012 8:04:10 PM


Reply: