【发布时间】:2015-05-28 16:28:22
【问题描述】:
为什么不允许丢弃 const 限定符?假设我们写了:
#include <iostream>
struct A
{
void operator=(const A&){ std::cout << "A&" << std::endl; }
void operator=(const A&&){ std::cout << "A&&" << std::endl; }
};
const A a;
A b;
int main()
{
a = b; //Error: discarding qualifier
}
不能在标准不允许的地方提供参考吗?
【问题讨论】:
-
为什么要允许?这样你就可以改变
const?那会让const变得毫无意义。 -
@juanchopanza 那么为什么下面的coliru.stacked-crooked.com/a/f094f8dad70b4c5f 完全有效?
-
正如 juanchopanza 所说。为什么在这种情况下使用
const?这似乎是在问为什么将int传递给需要std::string的函数时可能会出错。 -
@St.Antario 你认为你的例子有什么问题?
-
@St.Antario 因为
A::foo()是const。
标签: c++ class operator-keyword