|
|
assign to the 'this' pointer.
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: intent(out) for pointer dummy argument - comp.lang.fortran ...Furthermore, it can be (re)used if b is _not_ a pointer. If b is disassociated, assign_ptr1 doesn't care, while assign_ptr2 crashes at runtime, which yields a useful ... procedure() pointer - comp.lang.fortranHi, i am trying to do the following, with no success whatsoever: create a type (myType), that has a procedure() pointer. Then this pointer must po... Passing va_list by reference to a function - comp.lang.c ...... also didn't work so I created the > va_struct to pass a pointer tp a copy of ap. I don't think that the va_list type from <stdio.h> can be an array you cannot assign ... ISO C++ forbids assignment of arrays - is there a way out ? - comp ...Hi folks, How to assign an array in a constructor, when the array is needed in ... of arrays") Is there a way out, or do I have to give up and use a pointer to A as ... Assigning value to symbolic expressions in vector - comp.soft-sys ...I was hoping I could use some sort of pointer or reference. X(1) = R(1); just replaces the first element of X by the element of R, I want to assign the value of the ... const char ** syntax question - comp.lang.c++.moderatedFirstly, you shouldn't assign a string literal ("abc") to a pointer to non-const char. This is only supported for backward compatibility with old C dialects. How Do I Add a pointer or arrow to a picture ? - comp.graphics ...Hi all I have a picture of a car engine and I want to highlight a certain engine part by adding an arrow or pointer by the side of it to make it inst... Looking for simple way to assign IP for ad-hoc network - comp.os ...To free the user off the task to assign static IPs manually, I think the easiest way is to enable DHCP server at the PC running Windows 7, thus alll device will be ... moving the record pointer using RecordID - comp.databases ...After sorting the records again I wanted to be able to move the record pointer back to my RecordID but 36873 is beyond the scope of the currently found set of 385. SONET pointer adjustment - comp.dcom.sdh-sonetHi there, wow there are newsgroups for even the most evil stuff there is =) i got a little beginners question, plz help :x A SONET pointer is made ... Pointer Basics - Stanford CS Ed LibraryAssign the first pointer to point to the second pointee. This "loses" the reference to the first pointee which is unusual, but that's what the question calls for. How to Use Smart Pointers | eHow.comAssign the standard auto_ptr smart pointer for simplicity. It is often the best choice for local variables. Other options include the class member, though you can assign ... 7/21/2012 8:04:10 PM
|
|
|
|
|
|
|
|
|