C++:与符号“&"之间的区别和星号“*"在函数/方法声明中?

2022-01-05 00:00:00 reference pointers c++

它们之间是否存在某种细微差别:

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.

在此处查看我的回答以获得全面的所有差异的列表.

相关文章