c++ - 为什么 .* 运算符不能在 C++ 中重载?

标签 c++ operator-overloading

我发现这里的一些帖子讨论了运算符重载和不能在 c++ 中重载的运算符,例如 . :: .* sizeof 等。但是我找不到关于为什么要避免 .* 的确切细节或原因?你们中很少有人会认为它是重复的,但如果我能在这些链接上获得关于我想要的内容的详细信息,我会非常高兴:)

最佳答案

来自Horse's mouth :

Operator . (dot) could in principle be overloaded using the same technique as used for ->. However, doing so can lead to questions about whether an operation is meant for the object overloading . or an object referred to by . For example:

class Y {
public:
    void f();
    // ...
};

class X {   // assume that you can overload .
    Y* p;
    Y& operator.() { return *p; }
    void f();
    // ...
};

void g(X& x)
{
    x.f();  // X::f or Y::f or error?
}

This problem can be solved in several ways. At the time of standardization, it was not obvious which way would be best.

AFAIU 同样的推理适用于 .*

关于c++ - 为什么 .* 运算符不能在 C++ 中重载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12077322/

相关文章:

c++ - 高效获取数据的算法

c++ - 合并排序 k 列表 c++

c++ - 在这种情况下,运算符应该 == 还是 != 抛出?

c++ - 通过转换运算符推导模板参数类型

C++ - 如何为类模板声明函数模板友元

c++ - 在这种情况下,std::push_heap 的复杂度是 O(n) 而不是 O(logN) 吗?

c++ - 我可以为宏使用不同于逗号的参数分隔符吗?

Travis CI 上的 C++ 桌面 GUI 测试

c++ - 运算符重载优先级

python - `foo < bar < baz` 实际调用了哪些方法?