C ++:和号“&”之间的差异和星号“*”在函数/方法声明? [英] C++: difference between ampersand "&" and asterisk "*" in function/method declaration?

查看:202
本文介绍了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.

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

这篇关于C ++:和号“&”之间的差异和星号“*”在函数/方法声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆