In what case is it benificial to declare a method or function using const& instead of just using const? I know that using const is saying that this method does not modify the members of a class but what exactly happens when i add the reference sign after?
Example:
int myclass::getInteger() const& {
return theInt;
}
Is this a so called reference quailifier by the way? And more importantly what does the reference sign say about the method getInteger? Thanks!
Yes, this is a ref-qualified member function. The lvalue-reference notation on the right makes this function callable only on lvalues of myclass
, that is:
myclass c;
c.getInteger(); // OK
myclass{}.getInteger(); // Compile-time error
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With