-2
 MyClass & operator=(const MyClass &rhs);

What is the meaning of "&"? Why not is My Class instead of MyClass & ?

Proton
  • 1,335
  • 1
  • 10
  • 16
  • 2
    Probably very helpful: http://stackoverflow.com/questions/57483/what-are-the-differences-between-pointer-variable-and-reference-variable-in-c. By the way, googling "ampersand in C++ parameter" yields me tons of relevant results. – chris Mar 25 '13 at 04:31
  • 2
    I think you need to learn the basic fundamentals of C++. Try a [good book](http://stackoverflow.com/q/388242/10077). – Fred Larson Mar 25 '13 at 04:36

2 Answers2

2

The & simply means that the return value of operator = is a reference.

This has nothing to do with operator overloading. It's the same syntax with a normal function definition:

MyClass& foo()
{
  return *this; // returns a reference to MyClass instance
}

MyClass foo()
{
  return *this; // returns a copy of MyClass instance
}
Charles Salvia
  • 52,325
  • 13
  • 128
  • 140
  • And what meaning of `MyClass* foo()` ? – Proton Mar 25 '13 at 04:41
  • 2
    @DungProton, that means it returns a *pointer*. But this is really basic, fundamental stuff. Blindly asking this on stackoverflow probably isn't going to help you much. You should read a good introductory C++ book. – Charles Salvia Mar 25 '13 at 04:49
0

returning MyClass& is just to make operator '=' work in chain. Returning MyClass& or something different doesn't add any effect in overloading '='.

Returing MyClass& helps us use '=' similar the way we use it on built-in data types. eg.

int x = y = z = 123;

For similar discussion, refer Thinking in C++, (Discussion on iostreams).

null
  • 777
  • 1
  • 7
  • 12