Kay Kay - 1 year ago 127
C++ Question

Meaning of ampersand in method declaration

I know what a

qualifier means in a method declaration (making
const), but I can't fathom what the ampersands in these lines means:

MyClass &operator =(const MyClass&) & = default;
// ^- this one
bool operator ==(const MyClass &right) const &;
// that one -^

const MyClass&
? So what does the ampersand mean in here?

Answer Source

It means ref-qualified member functions:

A non-static member function can be declared with either an lvalue ref-qualifier (the token & after the function name) or rvalue ref-qualifier (the token && after the function name).

You can define both (lvalue/rvalue ref-qualifier) like:

bool operator ==(const MyClass &right) const &;
bool operator ==(const MyClass &right) const &&;

Note the type of *this won't change even in the rvalue ref-qualified function.

Note: unlike cv-qualification, ref-qualification does not change the properties of the this pointer: within a rvalue ref-qualified function, *this remains an lvalue expression.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download