为什么你不能超载'。运算符在C ++?

为什么你不能超载'。运算符在C ++?

问题描述:

这将是非常有用的能够重载。

It would be very useful to be able to overload the . operator in C++ and return a reference to an object.

您可以重载 operator-> 运算符* 但不是运算符。

这?

请参阅此报价来自Bjarne Stroustrup


运营商。 (点)原则上可以使用与用于 - >相同的
技术重载。然而,这样做可能导致问题
关于操作是否意味着对象重载。或者引用的
对象。例如:

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?
}

这个问题可以通过几种方式解决。在
标准化的时候,并不明显哪种方式是最好的。有关更多
的详情,请参见 C ++的设计和演变

This problem can be solved in several ways. At the time of standardization, it was not obvious which way would be best. For more details, see The Design and Evolution of C++.