Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does 'return *this' mean in C++?

Tags:

c++

pointers

this

I'm converting a C++ program to C#, but this part has me confused. What does return *this mean?

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#?

like image 463
Kloar Avatar asked Aug 10 '13 13:08

Kloar


People also ask

What does * this return?

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. Save this answer.

What does return () do in C?

A return statement ends the execution of a function, and returns control to the calling function. Execution resumes in the calling function at the point immediately following the call. A return statement can return a value to the calling function.

What is void * return type in C++?

When used as a function return type, the void keyword specifies that the function doesn't return a value. When used for a function's parameter list, void specifies that the function takes no parameters. When used in the declaration of a pointer, void specifies that the pointer is "universal."

What is the difference between the two statements return this and return * this?

“return *this” is going to return the current class object. And “return this” will return the object address of the current class. To be simple first statement will return something like return a, where a is a variable which will have some value in it.


1 Answers

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.

like image 119
Rahul Tripathi Avatar answered Oct 01 '22 07:10

Rahul Tripathi