就像this
指针用于将指针传递给当前对象一样,如何将引用传递给当前对象?
class UseMe
{
public:
UseMe_funcReference(base&);
UseMe_FuncPointer(base *)
}
class base{
..........
}
class Abc:public base
{
public:
friend UseMe;
UseMe innerObj;
Func1()
{
(this->innerObj).UseMe_funcPointer(this); // This passes pointer to current object
(this->innerObj).UseMe_funcReference(HOW_TO_PASS_REFERENCE_TO_CURRENT_OBJECT);
}
}
答案 0 :(得分:3)
您没有在调用网站上记录传递引用,您使用与常规按值传递完全相同的语法。
在实际函数中,使用&
表示参数是通过引用传递的。例如,考虑以foo
作为参考的函数bar
:
void foo(bar& reference);
如果foo(*this)
是this
(或相关)类型,则可以在呼叫网站上使用bar*
。
答案 1 :(得分:2)
您只需使用*this
,就像从赋值运算符返回对self的引用一样。
答案 2 :(得分:2)
this
是指向当前对象的指针。
*this
是当前对象的名称。
与其他人在这里所说的相反,它是不是一个引用(解除引用T*
给你一个左值T
),但是你可以绑定就像你在其他地方一样引用它。
tl; dr:取消引用它,就像你对任何其他指针一样