当我尝试将移动成员调用为按引用传递时,编译器会引发错误,但是当我重新定义成员函数以按值传递时,它可以工作。
我不能在成员函数中使用按引用传递作为右值吗?
#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
}