【发布时间】:2020-03-06 23:41:14
【问题描述】:
抱歉问题标题,但我不知道我的问题的正确标题。我有以下代码示例:
struct test {
test(int a) {
}
};
int main() {
test(1);
return 0;
}
上面的示例代码有效。现在我(以我的理解)做同样的事情有点不同:
struct test {
test(int a) {
}
};
int main() {
int a = 0;
test(a);
return 0;
}
编译时出现以下错误:
error: redefinition of 'a' with a different type: 'test' vs 'int'
但在我看来,当我尝试这个时,它会变得非常奇怪:
struct test {
test(int a) {
}
};
int main() {
int a = 0;
test((int)a);
return 0;
}
上面的例子再次起作用,真的让我很困惑,因为我看不出有什么区别(除了将 int 转换为 int)。谁能解释发生了什么?提前谢谢你。
【问题讨论】:
-
这被称为最麻烦的解析。 Clang 是 more helpful 而不是 GCC:
warning: parentheses were disambiguated as redundant parentheses around declaration of variable named 'a'note: add enclosing parentheses to perform a function-style cast
标签: c++