0

I saw this question on StackOverflow:

return "this" in C++?

And a question came to my mind - what's the difference between these three?

class myclass {
public:
   // Return by pointer needs const and non-const versions
         myclass* ReturnPointerToCurrentObject()       { return this; }
   const myclass* ReturnPointerToCurrentObject() const { return this; }

   // Return by reference needs const and non-const versions
         myclass& ReturnReferenceToCurrentObject()       { return *this; }
   const myclass& ReturnReferenceToCurrentObject() const { return *this; }

   // Return by value only needs one version.
   myclass ReturnCopyOfCurrentObject() const { return *this; }
};
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Wynell
  • 633
  • 1
  • 8
  • 15

1 Answers1

0

Take a close look at the following:

int i = 10;
int* ptr = &i;
int& ref = i;
int copy = i;

If you understand the differences between the last three lines, you can extend that understanding to the member functions in your posted code. If you don't understand the differences, it will be best to learn these basic concepts from a good textbook.

R Sahu
  • 204,454
  • 14
  • 159
  • 270