Jump to content
  • Advertisement
Sign in to follow this  
cpp forever

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

This topic is 4732 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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".

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
  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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 ptr

ptr = 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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!