[C++] Crazy idea, but... How to replace content of this

Started by
17 comments, last by rollo 18 years, 5 months ago
How to replace content of this pointer? I know it sounds crazy, but I have received question of how to do that from one of my friends and want to give him at least any "answer".
ai-blog.org: AI is discussed here.
Advertisement
Define "replace content of this pointer".
He's refering to the "this" pointer, as in a class pointer. Can you give us more details than that?
If you mean change what this points to, probably not.. The this pointer is designed to refer to that object, and you might be able to possibly get it to point to another object of the same type... Maybe a static or dynamic cast?
I don't know much on it, so don't count on me.
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
  1. Why would you want to do this?

  2. Don't do this.

  3. #include <new>this->~ClassName();new (this) Thing();

  4. Almost certainly undefined behaviour according to the standard.

  5. If the object is stack based and ClassName != Thing or if you delete the object as a ClassName not a Thing then you're looking at memory corruption.

  6. Don't do this.

  7. Cannot be made exception safe if the Thing constructor can throw.

  8. sizeof(Thing) must be <= sizeof(ClassName).

  9. Don't do this.

  10. Some other terrible problem I haven't thought of.

  11. Don't do this.

Enigma
Quote:Original post by dbzprogrammer
He's refering to the "this" pointer, as in a class pointer. Can you give us more details than that?
If you mean change what this points to, probably not.. The this pointer is designed to refer to that object, and you might be able to possibly get it to point to another object of the same type... Maybe a static or dynamic cast?
I don't know much on it, so don't count on me.


Well, I want do smth like this:
this = PointerToAnotherObjectOfTheSameType;

But it can't be compiled.
ai-blog.org: AI is discussed here.
You can safely call operator= if that's all you want to do:
class Thing{	public:		Thing & assignToSelf(Thing const & thing)		{			return operator=(thing);		}};

Enigma
It would be very compiler dependent if you wanted to try. For example, in theory, in MSVC you could do some operation to flush the ecx pointer, look at the disassembly of the function to see where the compiler stores the this pointer on the stack, and then use inline assembly to write a new pointer to that location.

But don't do this. Seriously it's a bad idea.
Re-locating the 'this' pointer is highly not recommended for your own sanity.

Are you trying to do something like this:
Thing* ptr = this;//do some stuff with ptrptr = ptr_to_some_other_Thing_object;//do some more stuff with ptr

Other than that I can't imagine what you'd want to do that for.
Making the this pointer point somewhere else from inside a running function is not allowed by the standard, I believe, as it's equivalent declaration is I believe as follows:

const Type* const this;

A const pointer (const after the *) cannot be modified at all. Its bits are set and aren't allowed to be changed.
daerid@gmail.com
Quote:Original post by daerid
Making the this pointer point somewhere else from inside a running function is not allowed by the standard, I believe, as it's equivalent declaration is I believe as follows:

const Type* const this;


Not quite. It's either "Type* const this;" or what you posted, depending on wheither or not the function is labeled const. The variation is on the constness of what's being pointed at, not the pointer itself, you still can't (or shouldn't be able to, anyways) reseat the "this" pointer.

This topic is closed to new replies.

Advertisement