美文网首页
M-引用参数强转

M-引用参数强转

作者: tarzipc | 来源:发表于2016-10-18 13:29 被阅读0次

之前写的代码里犯了个低级错误,编译时出现以下提示。

error: invalid initialization of non-const reference of type 'TYPEA &' from an rvalue of type 'TYPEB'

原因是对于类似以下的函数:
void refer(unsigned int &a);

我是这么调用的:
int a;
refer((unsigned int)a);   //掩面羞愧

refer((unsigned int&)a); //可以

类型转换将会产生一个你想要转换类型的一个临时变量。

不要做这种无意义的类型转换, 如果遇到这种情况, 考虑是否程序设计合理.

相关文章

网友评论

      本文标题:M-引用参数强转

      本文链接:https://www.haomeiwen.com/subject/oiftuttx.html