Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference sign after method declaration c++

Tags:

c++

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!

like image 391
Bonbin Avatar asked Jan 04 '17 16:01

Bonbin


1 Answers

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
like image 147
Quentin Avatar answered Sep 23 '22 03:09

Quentin