gpt4 book ai didi

c++ - C++中的引用

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

有一次我读到一份声明

The language feature that "sealed the deal" to include references is operator overloading.

为什么需要引用来有效支持运算符重载??有什么好的解释吗?

最佳答案

这是 Stroustrup 在“C++ 的设计与演化”(3.7“引用资料”)中所说的:

References were introduced primarily to support operator overloading. ...

C passes every function argument by value, and where passing an object by value would be inefficient or inappropriate the user can pass a pointer . This strategy doesn't work where operator overloading is used. In that case, notational convenience is essential because users cannot be expected to insert address-of operators if the objects are large. For example:

 a = b - c;

is acceptable (that is, conventional) notation, but

 a = &b - &c;

is not. Anyway, &b - &c already has a meaning in C, and I didn't want to change that.

关于c++ - C++中的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2570254/

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