Why isn't operator overloading for pointers allowed to work?

后端 未结 9 2047
鱼传尺愫
鱼传尺愫 2020-12-03 14:08

As per the comment under this answer, references were introduced primarily to support operator overloading which quotes Bjarne Stroustrup:

References

9条回答
  •  爱一瞬间的悲伤
    2020-12-03 14:47

    It's 2019 and you still do not have access to the classes of native types. There is no way to override operators on them.

    Pointers are (like char, int, float, etc...) a native type. You can create an object that behaves like a pointer and overload operators [->, *], but that will not overload [->, *] on the the native pointer. You may also, for example, create an object that behaves like an int and overload [+, -, *, /], but that will not overload [+, -, *, /] on the native int.

    For pragmatic rational, imagine the chaos if

    this->member
    

    had user-defined meaning.

    References are an unrelated topic, IMHO.

提交回复
热议问题