• Advertisement
Sign in to follow this  

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

This topic is 4460 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
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
why do you need to do this? wouldn't that just defeat the purpose of the 'this' pointer, because refering to this, as in the current class easily?

Share this post


Link to post
Share on other sites

class Test {
public:
void replaceThis(Test* t) {
this = t;
}
};


This will give you "error: non-lvalue in assignment" with g++

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
Sign in to follow this  

  • Advertisement