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

查看:15
本文介绍了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天全站免登陆