我的C老师认为标准C中的*运算符“已经重载”,因为它可以意味着依赖于上下文的间接或乘法.他从C Primer Plus获得了这一点,其中指出:
Actually,many C++ (and C) operators already are overloaded. For example,the * operator,when applied to an address,yields the value stored at that address. But applying * to two numbers yields the product of the values. C++ uses the number and type of operands to decide which action to take. (pg 502,5th ed)
至少另外一本教科书说得很一样.据我所知,这是不正确的;一元*是与二进制*不同的运算符,编译器消除它们的机制与运算符重载无关.
谁是对的
解决方法
两者都是正确的,因为问题取决于上下文和重载一词的含义.
“重载”可以具有“同一符号,不同含义”的常用含义,并允许使用包括间接和乘法的“*”以及任何用户定义的行为.
“重载”可以用于C的官方运算符重载功能,在这种情况下,间接和乘法确实是不同的.
附录:请参阅下面史蒂夫的评论,关于“令牌超载”的“令牌重载”.