When I try to call the move member as pass by reference, the compiler throws an error but when I redefine the member function to pass by value, it works.
Can I not use pass by reference as an rvalue in my member function?
#include <iostream>
#include <string>
class Screen{
private:
std::string contents;
using position = std::string::size_type;
position height,width,cursor_position;
public:
Screen() = default;
Screen& move(position&,position&); // Pass by reference
};
Screen& Screen::move(position& row,position& col)
{
(*this).cursor_position = (row * width) + col;
return *this;
}
int main() {
Screen myScreen;
myScreen.move(4,0); // This line gives a compile error
}
No, rvalue cannot be passed as non-const lvalue reference. It can be passed as const lvalue reference though because the compiler will create a temporary to point the lvalue to.