Change 'this' pointer of an object to point different object

匆匆过客 提交于 2021-02-06 09:57:25

问题


class C{
   //methods and properties
}

void C::some_method(C* b){
    delete this;
    this = b;     
}

This gives me follwing error when compiling:

error: lvalue required as left operand of assignment

My intention: Say there are objects a and b of class C. The contents of the class C can be very huge and field by field copying can be very costly. I want all the contents of 'a' to be replaced by 'b' in an economical way.

Will default copy constructor do the intended task?

I found something called 'move constructor' http://akrzemi1.wordpress.com/2011/08/11/move-constructor/

Perhaps, it might get the effect that I want.


回答1:


The this-pointer is an implicit pointer to the object in whose context you are working, you cannot reassign it.

According to Stroustrup's bible (The C++ Programming Language, 3rd edition I have) this is expressed as

C * const this

meaning you have a constant pointer to your class C, so the compiler will complain if you try to change it.

EDIT:

As I was corrected, the above mentioned expression does not describe this fully correctly, for this is actually an rvalue.




回答2:


You cannot change what this points to. I would also not know why you'd want to do this.




回答3:


To quote the standard:

In the body of a non-static (9.3) member function, the keyword this is a prvalue expression whose value is the address of the object for which the function is called.

A "prvalue" is a pure rvalue, something like 42 or 3.14159. In the same way you can't do something like 42 = x, you can't assign to this; in both cases (at least conceptually), there is no object whose value can change.

And I'm really curious as to what you expect to happen if I write something like:

int
main()
{
    C c1;
    C c2
    c1.some_method( &c2 );
}

Do you expect the address of c1 to somehow miraculously change, and for c1 and c2 to be aliases to the same object? (And c1.some_method( NULL ) is even more intreguing.)




回答4:


You can't assign a different value to this as it point to the object itself, and this haven't any sense. You could instantiate a new object and use its implicit this pointer instead.

Moreover, if you try to make a copy of object, you can overwrite operator=

class Foo
{
  public:
    [...]
    Foo& operator=(const Foo& foo);
}

int main()
{
 Foo foo;
 foobar = foo; //invoke operator= overwrited method
}





回答5:


The error says "You can't assign b to this". As far as I know, this is something you can't change, because it's not an actual pointer, but a self-reference.




回答6:


Just use the usual approach instead of black magic and UB:

C* c1 = new C();
C* c2 = new C();

// do some work perhaps ...

delete c1;
c1 = c2;

Now c1 is an alias to c2 as you wanted. Be careful though when cleaning up memory so you don't end up deleting an object twice. You might perhaps consider smart pointers...



来源:https://stackoverflow.com/questions/18227511/change-this-pointer-of-an-object-to-point-different-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!