'return *this' 在 C++ 中是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18162522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does 'return *this' mean in C++?
提问by Kloar
I'm converting a C++ program to C#, but this part has me confused. What does return *this mean?
我正在将 C++ 程序转换为 C#,但这部分让我感到困惑。return *this 是什么意思?
template< EDemoCommands msgType, typename PB_OBJECT_TYPE >
class CDemoMessagePB : public IDemoMessage, public PB_OBJECT_TYPE
{
(...)
virtual ::google::protobuf::Message& GetProtoMsg() { return *this; }
}
How would it translate into C#?
它将如何转换为 C#?
回答by Rahul Tripathi
this
means pointer to the object, so *this
is an object. So you are returning an object ie, *this
returns a reference to the object.
this
表示指向对象的指针,对象*this
也是。所以你正在返回一个对象,即*this
返回对该对象的引用。
回答by Stephen Brooks
Watch out that if you try to use return *this;
on a function whose return type is Type
and not Type&
, C++ will try to make a copy of the object and then immediately call the destructor, usually not the intended behaviour. So the return type should be a reference as in your example.
请注意,如果您尝试return *this;
在返回类型为Type
和 不是的函数上使用Type&
,C++ 将尝试制作该对象的副本,然后立即调用析构函数,通常不是预期的行为。所以返回类型应该是你的例子中的引用。
回答by Lake
In your particular case, you are returning the reference to 'this', since the return type of the function is a reference (&).
在您的特定情况下,您将返回对“this”的引用,因为函数的返回类型是引用(&)。
Speaking of the size of returned memory, it is the same as
说到返回内存的大小,和
virtual ::google::protobuf::Message* GetProtoMsg() { return this; }
But the usage at call time differs.
但是通话时的用法不同。
At call time, you will call store the return value of the function by something like:
在调用时,您将通过以下方式调用 store 函数的返回值:
Message& m = GetProtoMsg();
回答by Joseph Pla
You are just returning a reference to the object. this
is a pointer and you are dereferencing it.
您只是返回对对象的引用。this
是一个指针,您正在取消引用它。
It translates to C# return this;
in the case that you are not dealing with a primitive.
return this;
如果您不处理原语,它会转换为 C# 。
回答by Ehsan
Using a pointer we can directly access the value stored in the variable which it points to. To do this, we simply have to precede the pointer's identifier with an asterisk (*), which acts as dereference
operator and that can be literally translated to "value pointed by".
使用指针,我们可以直接访问存储在它指向的变量中的值。要做到这一点,我们只需在指针的标识符前面加上一个星号 (*),它充当dereference
运算符,可以直接翻译为“指向的值”。
回答by bash.d
Like in C# this
is an implicit pointer to the object you are currently using.
In your particular case, as you return a reference &
to the object, you must use *this
if you want to return the object you are currently working on.
Don't forget that a reference takes the variable itself, or in case of a pointer (this
), the object pointed to (*this
), but not the pointer (this
).
就像在 C# 中一样,this
是指向您当前使用的对象的隐式指针。
在您的特定情况下,当您返回&
对对象的引用时,*this
如果要返回当前正在处理的对象,则必须使用。
不要忘记引用采用变量本身,或者在指针 ( this
) 的情况下,是指向 ( ) 的对象*this
,而不是指针 ( this
)。