第2章 变量和基本类型
Posted COCO_PEAK_NOODLE
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第2章 变量和基本类型相关的知识,希望对你有一定的参考价值。
1-C++ 中的auto、auto&、auto&&、const auto&
#include <iostream>
int main()
{
int i = 0, &r = i;
auto a = r; // a is an int (r is an alias for i, which has type int)
const int ci = i, &cr = ci;
auto b = ci; // b is an int (top-level const in ci is dropped)
auto c = cr; // c is an int (cr is an alias for ci whose const is top-level)
auto d = &i; // d is an int* (& ofan int objectis int*)
auto e = &ci; // e is const int*(& of a const object is low-level const)
const auto f = ci; // deduced type of ci is int; f has type const int
auto &g = ci; // g is a const int& that is bound to ci
a = 42; b = 42; c = 42; *d = 42; e = &c;
return 0;
}
2-cin直接输入给变量
#include <iostream>
#include "ex2_42.h"
int main()
{
Sales_data book;
double price;
std::cin >> book.bookNo >> book.units_sold >> price;
book.CalcRevenue(price);
book.Print();
return 0;
}
3- 顶层const的概念
指针本身是一个对象,它又可以指向另外一个对象,因此,指针本身是不是常量以及指针所指的是不是一个常量就是两个互相独立的问题,用名词顶层const表示指针本身是一个常量,而用名词底层const表示指针所指的对象是一个常量。
以上是关于第2章 变量和基本类型的主要内容,如果未能解决你的问题,请参考以下文章