Objective
C + + syntax details are too miscellaneous, coupled with the new features of c++11, read and forget, forget to look again, so the learning process can be recorded in the form of notes.
1. The form of C + + for variable initialization
int a = 0; int a = {0}; int a (0); int a{0};//is called list initialization c++11, it can be fully applied
A long double b = 1.02; int a (b); int a=b; Correct: conversion is performed, but partial values are missing
int a{b}; int a={b};//Error: The conversion was not performed because there is a risk of losing information
2. Relationship of variable declaration and definition
extern int x; for declaring int x; To define
Any declaration that contains a display initialization is defined as: extern int a=8; Assigning an initial value to a has offset the effect of extern.
initializing extern tag variables inside the function body throws an error
3. Constants
Const Qualifier
Const a=2; In preprocessing, the compiler will say that all a is replaced with a (similar to a macro definition)
By default, the const object is only valid within the file. When a const variable of the same name appears in multiple files, it is actually equivalent to defining the variable at the same time in multiple files
If you want to use multiple files once to define multiple declarations, you must define and declare them using extern to qualify
a const reference
const int i=2;
const INT &r=i;//Correct
&r=i+1; Error
Since the constant value is not allowed to change, of course, it cannot be changed by reference, so the reference must also be a const type
top-level const and underlying const const int A= 10;//top-level const
int b=10;
int *const p=b;//Underlying const
It is simple to say that variable objects, which are directly modified with const, are top-level const and pointers and references to const variables or objects are the underlying const
Special: const int a=10; const int *const p=a; At this point P is both the top and bottom const
4. Constant expression
An expression that indicates that the value will not change and that the result will be evaluated during compilation
int a=1;//is not a const int b=2;//is
const INT c=b+2;//is a const int d=a+b;//not
5.auto type specifier
If you do not know at the time of declaration that the value and type of the variable initialization can be used
auto int a=5,b=4;
Auto C = a+b;
The compiler calculates the type of the variable through the value of the a+b, so the variable defined by auto must have an initial value
6.decltype Type Indicator
If you want to get the variable type from an expression, but it is not initialized with the value of the variable, you can use the Decltype type prompt
Usage decltype (x) sum = 11;
X can be a function f () can be a variable can be an expression special X can be a reference reference is always made as an alias for the object it refers to appear
Only here is an accident if int A; Decltype ((a)) B; Then B is the reference type
Because variable A is a special expression that can be an lvalue of an assignment statement , the type of (a) is a reference
7. Type aliases
typedef typedef A B aliases B as a
Using X=y; alias X as Y
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C + + Learning notes (partial syntax and c++11 new features)