C ++:和号“&”之间的差异和星号“*”在函数/方法声明?

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.

有关所有差异的完整列表,请参见我的答案