gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 23:46:19 24 4
gpt4 key购买 nike

我发现这里的一些帖子讨论了运算符重载和不能在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com