C++: difference between ampersand quot;amp;quot; and asterisk quot;*quot; in function/method declaration?(C++:与符号“amp;之间的区别和星号“*在函数/方法声明中?)
问题描述
它们之间是否存在某种细微差别:
Is there some kind of subtle difference between those:
void a1(float &b) {
b=1;
};
a1(b);
和
void a1(float *b) {
(*b)=1;
};
a1(&b);
?
他们都做同样的事情(或者从 main() 看来是这样),但第一个显然更短,但是我看到的大部分代码都使用第二个符号.有区别吗?也许以防万一它是某个对象而不是浮动?
They both do the same (or so it seems from main() ), but the first one is obviously shorter, however most of the code I see uses second notation. Is there a difference? Maybe in case it's some object instead of float?
推荐答案
两者的作用相同,但一个使用引用,一个使用指针.
Both do the same, but one uses references and one uses pointers.
在此处查看我的回答以获得全面的所有差异的列表.
这篇关于C++:与符号“&"之间的区别和星号“*"在函数/方法声明中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++:与符号“&"之间的区别和星号“*"在函数/方法声明中?


- 从python回调到c++的选项 2022-11-16
- 近似搜索的工作原理 2021-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 静态初始化顺序失败 2022-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- C++ 协变模板 2021-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01