一、auto意义

  编程时常常需要把表达式的值赋给变量,这就要求在声明变量的时候清楚地知道表达式的类型,然后要做到这一点并非那么容易。为了解决这个问题,C++11新标准引入了auto类型说明符,用它就能让编译器替我们去分析表达式所属的类型。

二、auto用法

  1.基本用法

    int tempA = ;
int tempB = ;
/*1.正常推断auto为int,编译通过*/
auto autoTempA = tempA + tempB;
/*2.正常推断auto为int,编译通过*/
auto autoTempB = , *autoTempC = &autoTempB;
/*3.autoTempD推断为int,autoTempE推断为double,编译不过*/
auto autoTempD = , autoTempE = 3.14;

  2.与const结合

    const int ctempA = ;
auto autoTempA = ctempA; /*1.cautoTempA推断为int,但是手动加了const,所以cautoTempA最终类型为const int*/
const auto cautoTempA = ctempA;
/*2.autoTempA推断为int,忽略顶层const*/
autoTempA = ;

  3.与引用结合

    int tempA = ;
int &refTempA = tempA;
/*1.忽略引用,autoTempA推断为int,refAutoTempA被手动置为引用*/
auto autoTempA = refTempA;
auto &refAutoTempA = refTempA; autoTempA = ;
refAutoTempA = ;
/*2.输出为3,3,4,3*/
cout<<"tempA = "<<tempA<<endl;
cout<<"refTempA = "<<refTempA<<endl;
cout<<"autoTempA = "<<autoTempA<<endl;
cout<<"refAutoTempA = "<<refAutoTempA<<endl;

  4.与指针结合

   int tempA = ;
const int ctempA = ; /*1.ptrTempA中auto推断为int*,ptrTempB中推断为int */
auto ptrTempA = &tempA;
auto *ptrTempB = &tempA;
/*2.cptrTempA中auto推断为const int*,cptrTempB中推断为cosnt int */
auto cptrTempA = &ctempA;
auto *cptrTempB = &ctempA;
/*3.ptrTempA和ptrTempB输出完全一致*/
cout<<" ptrTempA = "<<ptrTempA<<endl;
cout<<"*ptrTempA = "<<*ptrTempA<<endl;
cout<<" ptrTempB = "<<ptrTempB<<endl;
cout<<"*ptrTempB = "<<*ptrTempB<<endl;
/*4.cptrTempA和cptrTempB输出完全一致*/
cout<<" cptrTempA = "<<cptrTempA<<endl;
cout<<"*cptrTempA = "<<*cptrTempA<<endl;
cout<<" cptrTempB = "<<cptrTempB<<endl;
cout<<"*cptrTempB = "<<*cptrTempB<<endl;
/*5.cptrTempA指向的为const int,不能通过cptrTempA来修改其值,编译不过*/
*cptrTempA = ;

三、auto使用总结

  上面的例子只是为了说明auto与const、引用和指针的用法,在实际工作中,auto主要还是为了简化一些复杂的声明;但是建议在使用时必须要清楚自己auto出来的类型到底是什么,这样才能做到心中有数,出现问题才能快速定位。

C++11新标准:auto关键字的更多相关文章

  1. C++11新标准:decltype关键字

    一.decltype意义 有时我们希望从表达式的类型推断出要定义的变量类型,但是不想用该表达式的值初始化变量(如果要初始化就用auto了).为了满足这一需求,C++11新标准引入了decltype类型 ...

  2. C++11新标准:nullptr关键字

    一.nullptr的意义 1.NULL在C中的定义 #define NULL (void*)0 2.NULL在C++中的定义 #ifndef NULL #ifdef __cplusplus #defi ...

  3. C++11新标准学习

    <深入理解C++11:C++11新特性解析与应用> <华章科技:深入理解C++11:C++11新特性解析与应用>一共8章:第1章从设计思维和应用范畴两个维度对C++11新标准中 ...

  4. C++11新特性— auto 和 decltype 区别和联系

    一. auto简介 编程时候常常需要把表达式的值付给变量,需要在声明变量的时候清楚的知道变量是什么类型.然而做到这一点并非那么容易(特别是模板中),有时候根本做不到.为了解决这个问题,C++11新标准 ...

  5. C++11特性:auto关键字

    前言 本文的内容已经不新鲜了.关于auto,翻来覆去被人知道的都是这些东西,本文并没有提出新颖的auto用法. 本人原是痛恨博客一篇篇都是copy而来缺乏新意的探索,当然,本文不是copy而来,但发布 ...

  6. c++11新标准for循环和lambda表达式

    :first-child { margin-top: 0px; } .markdown-preview:not([data-use-github-style]) h1, .markdown-previ ...

  7. C++11 - 类型推导auto关键字

    在C++11中,auto关键字被作为类型自动类型推导关键字 (1)基本用法 C++98:类型 变量名 = 初值;   int i = 10; C++11:auto 变量名 = 初值;  auto i ...

  8. 关注C++细节——C++11新标准之decltype的使用注意

    c++11新特性--decltype decltype是C++11加入的一个新的keyword,目的是选择并返回操作数的数据类型,重要的是,在此过程中编译器分析表达式并得到它的类型,却不实际计算表达式 ...

  9. C++11新标准:constexpr关键字

    一.constexpr意义 将变量声明为constexpr类型以便由编译器来验证变量是否是一个常量表达式(不会改变,在编译过程中就能得到计算结果的表达式).是一种比const更强的约束,这样可以得到更 ...

随机推荐

  1. 转: 全局变量报错:UnboundLocalError: local variable 'l' referenced before assignment

    http://blog.csdn.net/my2010sam/article/details/17735159

  2. Hive的JDBC访问

    实现hive查询源码: String driverName = "org.apache.hive.jdbc.HiveDriver"; try { Class.forName(dri ...

  3. jenkins插件

    构建maven项目:Maven Release Plug-in Plug-in

  4. phyton方面相关书籍

    0基础:<简明PYTHON教程>http://linux.chinaitlab.com/manual/Python_chinese/<与孩子一起学编程>http://book. ...

  5. 发RTX通知

    安装sdk 在RTXServer目录下找到WebRoot目录,找到里面的SendNotify.cgi(就是一个php页面,默认是pc - ascii编码).打开页面,在头部加上编码信息 header( ...

  6. C Primer Plus学习笔记(四)- 运算符、表达式和语句

    基本运算符 赋值运算符:= 在C语言中,=不是“相等”,而是赋值运算符,把左边的值赋给右边的变量 a = 2018; //把值2018赋给变量a 赋值表达式语句的目的是把值储存到内存位置上,用于储存值 ...

  7. ansible for devops读书笔记第一章

    yum -y install ansible ansible --version mkdir /etc/ansible touch /etc/ansible/hosts [example]   www ...

  8. LAMP 3.3 mysql常用操作-1

    有一个图形化管理 mysql 的工具叫做 phpmyadmin,如何在命令行下面来管理和操作 mysql. 首先进入mysql mysql -uroot -pwangshaojun 查看有那些库 &g ...

  9. VS2015 MSVC编译FFMPEG

    1.下载安装msys2 http://www.msys2.org/下载msys2 下载安装完成后,在msys2的shell中安装编译FFMPEG必要的命令行工具 pacman -S make gcc ...

  10. DAY11-MYSQL多表查询

    一 介绍 本节主题 多表连接查询 复合条件连接查询 子查询 准备表 #建表 create table department( id int, name ) ); create table employ ...