Sign in to follow this  
cpp forever

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

Recommended Posts

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
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
Query: If you're not supposed to be able to reseat the this-pointer, and it shouldn't ever be null or invalid (both of which are sane things to ask of it) - then why isn't it a reference instead? Silly C++ standard... ::mutter::

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Query: If you're not supposed to be able to reseat the this-pointer, and it shouldn't ever be null or invalid (both of which are sane things to ask of it) - then why isn't it a reference instead? Silly C++ standard... ::mutter::


Because C++ "grew" from C, rather than being nice and standardized from the start. If you look at the history, you'll note a 15 year gap between it's inception and it's ISO standardization. I have the feeling "this" came first - references more likely an aftereffect of operator overloading syntax rather than anything else (e.g. "operator++ certainly shouldn't return a pointer to this, so we'll need some newfangled reference so we can return a modifiyable version of *this.")

This is what I'm assuming, anyways.

Share this post


Link to post
Share on other sites
1. Don't do this.
2. Chance are it's not possible. The 'this' pointer is merely an implicit first parameter to all non-static member function, under the hood.
That being said a non-static member function foobar:
void foo::foobar(int bar)
would essentially be like this:
void foobar(foo *this, int bar)
However to do what you want would essentially require it to be implemented as a reference like this
void foobar(foo *&this, int bar)
But since that would be pointless and stupid, you can safely assume that it wont be like that, and thus it wouldn't be possible.

Hell, if it weren't for maintaining some compatibility with C, 'this' itself would be a reference. Now have you ever heard of a reference to a reference? I don't think so!

[Edited by - iMalc on November 4, 2005 2:48:14 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
not so long ago i successfully wrote something (actually a quite a bit different) like

struct test
{
test(test* t) { *this = *t; }
~test() {}
size_t data;
};

test *make_test() { .. alloc and return a test };

test t(make_test);

using intel c++9 and vc8 this compiles fine. is that what you meant?

regards,
simon.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
i know the above leaks memory etc.. my case was different. just to show the *this = *ptr thing.

simon

Share this post


Link to post
Share on other sites
Quote:
Original post by Enigma
  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


Excellent post. LOL

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
just to show the *this = *ptr thing.


The reason it works is because you are using *this = *ptr and not this = ptr. Your version isnt trying to change the pointer and it basically does this:
this->operator=(ptr) using the default assignment operator generated by the compiler.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this