gpt4 book ai didi

c++ - 默认编译器生成引用运算符(在 C++ 中)?

转载 作者:搜寻专家 更新时间:2023-10-31 01:48:11 24 4
gpt4 key购买 nike

所有作者都写道,默认情况下,编译器生成

1. the default constructor
2. copy constructor
3. assignment operator
4. destructor

前几天,我的一位 friend 应邀参加面试,他被告知默认情况下,编译器仍会生成一个函数引用运算符(运算符 &())。事实上是这样吗?

最佳答案

不,那不是真的。如果不存在 operator&,则使用核心语言中的那个。

N3485 13.3.1.2 [over.match.oper]/1-2(强调我的):

If no operand of an operator in an expression has a type that is a class or an enumeration, the operator is assumed to be a built-in operator and interpreted according to Clause 5. [ Note: Because ., .*, and :: cannot be overloaded, these operators are always built-in operators interpreted according to Clause 5. ?: cannot be overloaded, but the rules in this subclause are used to determine the conversions to be applied to the second and third operands when they have class or enumeration type (5.16). —end note ]

If either operand has a type that is a class or an enumeration, a user-defined operator function might be declared that implements this operator or a user-defined conversion can be necessary to convert the operand to a type that is appropriate for a built-in operator. In this case, overload resolution is used to determine which operator function or built-in operator is to be invoked to implement the operator.

您还可以看到,为您隐式声明的函数被列为“特殊成员函数(第 12 条),它没有引用 operator&

关于c++ - 默认编译器生成引用运算符(在 C++ 中)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18201645/

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