在这里我理解rhs意味着右手边但我不明白编译器如何理解"rhs"指的是右手边.并且有人可以解释在哪种情况下这种重载是必要的吗?
MyArray<T>& operator=(const MyArray<T>& rhs);
Run Code Online (Sandbox Code Playgroud)
Cha*_*had 12
编译器不知道它rhs代表"右侧",实际上该变量的名称可以是您喜欢的任何名称.
编译器"知道"如何格式化它,因为语法operator=要求它是这样的.
class A
{
public:
A& operator=(const A& other);
};
Run Code Online (Sandbox Code Playgroud)
该语言定义了此运算符的用法以采用以下形式:
A a, b;
a = b;
Run Code Online (Sandbox Code Playgroud)
上面的代码调用named A::operator=(const &other)的实例,并使用名为as 的实例.AaAbother