What does assignment to *this do (*this = val)?

久未见 提交于 2019-12-07 01:40:18

问题


I was browsing Qt sources, and noticed this

QUuid &operator=(const GUID &guid)
{
    *this = QUuid(guid);
    return *this;
}

I've never seen assignment to "this" before. What does assignment to "this" do?


回答1:


That is not an assignment to this but to the object pointed by this. That will effectively call operator=( QUuid const & ) on the current object.




回答2:


It just invokes QUuid &operator=(const QUuid& quUid);.




回答3:


'this' is simply a pointer to the object on which the current method is invoked. Changing the value behind 'this' (by dereferencing the pointer using '*this' and assigning another object) modifies the caller's object to become another one.

In your example, a caller of 'operator=' might do the following:

GUID guid = guid(...) ;
QUuid uid = guid ;

According to the definition of 'operator=' this action copy-converts 'guid' into a new object of type 'QUuid'.



来源:https://stackoverflow.com/questions/4734241/what-does-assignment-to-this-do-this-val

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