具有和不具有指针声明符的C ++ 11自动声明 [英] C++11 auto declaration with and without pointer declarator

查看:84
本文介绍了具有和不具有指针声明符的C ++ 11自动声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

bar1bar2的类型有什么区别?

What's the difference between the types of bar1 and bar2?

int foo = 10;
auto bar1 = &foo;
auto *bar2 = &foo;

如果bar1bar2均为int*,在bar2声明中写入指针声明符(*)是否有意义?

If both bar1 and bar2 are int*, does it makes sense to write the pointer declarator (*) in the bar2 declaration?

推荐答案

声明完全相同. auto的工作原理(几乎)与模板类型推论相同.明确地加星号使代码更易于阅读,并使程序员意识到bar2是指针.

The declarations are exactly equivalent. auto works (almost) the same as template type deduction. Putting the star explicitly makes the code a bit easier to read, and makes the programmer aware that bar2 is a pointer.

这篇关于具有和不具有指针声明符的C ++ 11自动声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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