- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我对 C++ 的这种行为感到困惑:
struct A {
virtual void print() const { printf("a\n"); }
};
struct B : public A {
virtual void print() const { printf("b\n"); }
};
struct C {
operator B() { return B(); }
};
void print(const A& a) {
a.print();
}
int main() {
C c;
print(c);
}
那么,测验是,程序的输出是什么 - a 还是 b?嗯,答案是一个。但为什么呢?
最佳答案
这里的问题是 C++03 标准中的一个错误/错误功能/漏洞,不同的编译器试图以不同的方式修补该问题。 (这个问题在 C++11 标准中已经不存在了。)
这两个标准的第 8.5.3/5 节指定了如何初始化引用。这是 C++03 版本(列表编号是我的):
A reference to type
cv1 T1
is initialized by an expression of typecv2 T2
as follows:
If the initializer expression
- is an lvalue (but is not a bit-field), and “cv1 T1” is reference-compatible with “cv2 T2,” or
- has a class type (i.e.,
T2
is a class type) and can be implicitly converted to an lvalue of typecv3 T3
, wherecv1 T1
is reference-compatible withcv3 T3
then the reference is bound directly to the initializer expression lvalue in the first case, and the reference is bound to the lvalue result of the conversion in the second case.
Otherwise, the reference shall be to a non-volatile const type (i.e.,
cv1
shall beconst
).If the initializer expression is an rvalue, with
T2
a class type, andcv1 T1
is reference-compatible withcv2 T2
, the reference is bound in one of the following ways (the choice is implementation-defined):
- The reference is bound to the object represented by the rvalue (see 3.10) or to a sub-object within that object.
- A temporary of type
cv1 T2
[sic] is created, and a constructor is called to copy the entire rvalue object into the temporary. The reference is bound to the temporary or to a sub-object within the temporary.The constructor that would be used to make the copy shall be callable whether or not the copy is actually done.
Otherwise, a temporary of type
cv1 T1
is created and initialized from the initializer expression using the rules for a non-reference copy initialization (8.5). The reference is then bound to the temporary.
手头的问题涉及三种类型:
T1
。在这种情况下,它是 struct A
。T2
。在这种情况下,初始化表达式是变量 c
,所以 T2
是 struct C
。请注意,由于 struct A
与 struct C
不是 reference-compatible,因此无法直接将引用绑定(bind)到 c
。需要一个中间人。T3
。在这种情况下,这是 struct B
。请注意,将转换运算符 C::operator B()
应用于 c
会将左值 c
转换为右值。我标记为 1.1 和 3 的初始化已经结束,因为 struct A
与 struct C
的引用不兼容。需要使用转换运算符C::operator B()
。 1.2 out 因为这个转换运算符返回一个右值,所以这个规则 1.2 out。剩下的就是选项 4,创建一个 cv1 T1
类型的临时文件。严格遵守 2003 版标准会强制为这个问题创建两个临时文件,即使只有一个就足够了。
2011 版标准通过将选项 3 替换为
解决了该问题
If the initializer expression
- is an xvalue, class prvalue, array prvalue or function lvalue and
cv1 T1
is reference- compatible withcv2 T2
, or- has a class type (i.e.,
T2
is a class type), whereT1
is not reference-related toT2
, and can be implicitly converted to an xvalue, class prvalue, or function lvalue of typecv3 T3
, wherecv1 T1
is reference-compatible withcv3 T3
,then the reference is bound to the value of the initializer expression in the first case and to the result of the conversion in the second case (or, in either case, to an appropriate base class subobject). In the second case, if the reference is an rvalue reference and the second standard con- version sequence of the user-defined conversion sequence includes an lvalue-to-rvalue conversion, the program is ill-formed.
似乎 gcc 系列编译器选择了严格遵从而不是意图(避免创建不必要的临时变量),而其他打印“b”的编译器选择了对标准的意图/更正。选择严格合规不一定值得称道。在 2003 版标准(例如 std::set
)中还有其他错误/错误功能,其中 gcc 家族选择理智而不是严格遵守。
关于C++ 强制转换运算符重载和多态性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14298715/
Or 运算符 对两个表达式进行逻辑“或”运算。 result = expression1 Or expression2 参数 result 任意数值变量。 expression1 任意
Not 运算符 对表达式执行逻辑非运算。 result = Not expression 参数 result 任意数值变量。 expression 任意表达式。 说明 下表显示如何
Is 运算符 比较两个对象引用变量。 result = object1 Is object2 参数 result 任意数值变量。 object1 任意对象名。 object2 任意
\ 运算符 两个数相除并返回以整数形式表示的结果。 result = number1\number2 参数 result 任意数值变量。 number1 任意数值表达式。 numbe
And 运算符 对两个表达式进行逻辑“与”运算。 result = expression1 And expression2 参数 result 任意数值变量。 expression1
运算符(+) 计算两个数之和。 result = expression1 + expression2 参数 result 任意数值变量。 expression1 任意表达式。 exp
我对此感到困惑snippet : var n1 = 5-"4"; var n2 = 5+"4"; alert(n1); alert(n2); 我知道 n1 是 1。那是因为减号运算符会将字符串“4”转
我想我会得到 12,而不是 7。 w++,那么w就是4,也就是100,而w++, w 将是 8,1000;所以 w++|z++ 将是 100|1000 = 1100 将是 12。 我怎么了? int
Xor 运算符 对两个表达式进行逻辑“异或”运算。 result = expression1 Xor expression2 参数 result 任意数值变量。 expression1
Mod 运算符 两个数值相除并返回其余数。 result = number1 Mod number2 参数 result 任意数值变量。 number1 任意数值表达式。 numbe
Imp 运算符 对两个表达式进行逻辑蕴涵运算。 result = expression1 Imp expression2 参数 result 任意数值变量。 expression1 任
Eqv 运算符 执行两个表达式的逻辑等价运算。 result = expression1 Eqv expression2 参数 result 任意数值变量。 expression1 任
我有一个运算符重载的简单数学 vector 类。我想为我的运算符(operator)获取一些计时结果。我可以通过计时以下代码轻松计时我的 +=、-=、*= 和/=: Vector sum; for(s
我是用户定义比较运算符的新手。我正在读一本书,其中提到了以下示例: struct P { int x, y; bool operator、运算符<等),我们
在 SQL 的维基百科页面上,有一些关于 SQL 中 bool 逻辑的真值表。 [1] 维基百科页面似乎来源于 SQL:2003 标准。 等号运算符 (=) 的真值表与 SQL:2003 草案中的 I
我遇到了一个奇怪的 C++ 运算符。 http://www.terralib.org/html/v410/classoracle_1_1occi_1_1_number.html#a0f2780081f
我正在阅读关于 SO 和 answers 中的一个问题,它被提到为: If no unambiguous matching deallocation function can be found, pr
我偶然发现了这个解决方案,但我无法理解其中到底发生了什么。谁能解释一下! 据我了解,它试图通过计算一半的单元格然后将其加倍来计算 a*b 网格中的单元格数量。但是我无法理解递归调用。 请不要建议其他解
Go的基本类型 布尔类型bool 长度:1字节 取值:布尔类型的取值只能是true或者false,不能用数字来表示 整型 通用整型 int / uint(有符号 / 无符号,下面也类似) 长度:根据运
在本教程中,您将学习JavaScript中可用的不同运算符,以及在示例的帮助下如何使用它们。 什么是运算符? 在JavaScript中,运算符是一种特殊符号,用于对运算数(值和变量)执行操作。例如,
我是一名优秀的程序员,十分优秀!