Variables and Basic Types of c++ primer

Keywords: pip

If there are both symbolic and unsigned types in the expression, the abnormal result will appear when the value of the symbolic type is negative. Because signed numbers are automatically converted to unsigned numbers.

unsigned u=10;
int i=-42;
std::cout<<i+i<<std::endl;//output-84
std::cout<<u+i<<std::endl;//If int occupy32Bit output4294967264

Reference and pointer:

Reference is not an object. Reference is just another name for an existing object.

The pointer itself is an object, allowing assignment and copy of the pointer, and it can point to different objects successively in the life cycle of the pointer.

Initial values are assigned when pointers need not be redefined. But pointers within the block scope definition must be initialized.

Use the equality operator (==) or! = When the operator compares pointers, it compares the address values stored by the two pointers.

Reference to pointer:

A reference is not an object in itself, so you cannot define a pointer to a reference. But pointers are objects, so there are references to pointers.

int i=42;
int *p=0;//p is an int pointer
int *&r=p;//r is a reference to pointer p
r=&i;//Make p point to i
*r=0;//Let the dereference r get i, i=0;

When faced with a more complex pointer or quoted statement, reading from right to left helps to clarify its true meaning.

Reference to const:

Constant references limit only the operations in which references can participate, and whether the referenced object itself is a constant.

The pointer to const:

A pointer to a constant cannot be used to change the value of the object it refers to. To store the address of a constant object, you can only use a pointer to a constant.

const double pi=3.14;//pi is a constant, the value cannot be changed
double *ptr=&pi;//Error, ptr is a common pointer
const double *cptr=&pi;//A pointer to a constant points to a constant object
*ptr=42;//Error, cannot assign * ptr
double dval=3.14;
cptr=&dval;//Correct, see note.

Note: A pointer to a constant does not specify that the object it refers to must be a constant. The so-called pointer to a constant only requires that the value of an object cannot be changed by the pointer, but does not require that the value of that object cannot be changed by other means.

const pointer

A constant pointer means that the pointer itself is a constant and must be initialized. Once initialization is completed, its value, that is, the address stored in the pointer, cannot be changed. Represented as * const, unchanged is the value of the pointer itself, not the value pointed to.

int errNumb=0;
int *const currErr=&errNumb;//currErr always points to errNumb
const double pi=3.14159;
const double *const pip=&pi;//pip is a constant pointer to a constant object
*pip=2.172;//You cannot modify the value of an object by pointing to a constant object
*curErr=0;//You can reset the curErr pointer to an object because it points to a non-constant object.

The top const indicates that the pointer itself is a constant.

The underlying const indicates that the object pointed to by the pointer is a constant.

int i=0;
int *const p1=&i;//Top level const
const int ci=42;//Top level const
const int *p2=&ci;//Bottom const

When performing a copy operation of an object, the difference between the top const and the bottom const is obvious.

The top const is not affected.

i=ci;//Correct
p2=p3;//Correct

When performing a copy operation of an object, the copy-in and copy-out objects must have the same underlying const qualification, or the data types of the two objects must be able to be converted.

int *p=p3;//Mistake, p3 Include the bottom layer const Definition, and p No,
p2=p3;//Correct
p2=&i;//Correct,int*canTransform into const int*
int &r=ci;//Error, commonint&Can't be bound tointOn constants
const int &r2=i;//Correct: const int&Can be bound to ordinaryintupper

constexpr and Constant Expressions:

Constant expression is that the value does not change and the expression of calculation results can be obtained during the compilation process.

We allow a variable to be declared as a constexpr type so that the compiler can verify whether the value of the variable is a constant expression. A variable declared as constexpr must be a constant and must be initialized with a constant expression.

If a pointer is defined in a constexpr declaration, the qualifier constexpr is only valid for the pointer and is independent of the object pointed to by the pointer.

const int *p=nullptr;//p is a pointer to integer constants
constexpr int *q=nullptr;//q is a constant pointer to an integer

It should be noted that const is a modification of a given type. When using type aliases, a type alias is a type.

typedef char *pstring;//pstring is char*
const pstring cstr=0;//cstr is a constant pointer to char
const pstring *ps;//ps is a pointer whose object is a constant pointer to char

The new C++11 standard introduces the auto type descriptor, which allows the compiler to analyze the type of expression for us.

(1) When inferring a reference, the compiler takes the type of the reference object as the type of auto.

(2) auto generally ignores the top const, while the bottom const remains.

(3) If we want to infer that the auto type is a top const, we need to explicitly point it out.

decltype type type indicator: Selects and returns the data type of the operand.

decltype(f()) sum=x;//The type of sum is the return type of function f. The compiler parses the expression and gets its type, but does not actually compute the value of the expression.

If the expression used by decltype is a variable, decltype returns the type of the variable (including the top const and reference).

const int ci=0,&cj=ci;
decltype(ci) x=0;//The type of x is const int
decltype(cj) y=x;//The type of Y is const int &, y is bound to variable x
decltype(cj) z;//Error, z is a reference and must be initialized.

If the expression used by decltype is not a variable, decltype returns the type corresponding to the result of the expression.

int i=42,*p=&i,&r=i;
decltype(r+0) b;//Correct:The result of addition isint,therefore b Is a (uninitialized)int
decltype(*p) c;//Mistake, c yesint&,It must be initialized. If the content of an expression is a dereference operation, then decltype The reference type will be obtained.

The result of decltype ((variable) (note double parentheses) is always a reference, while the result of decltype(variable) is a reference only when the variable itself is a reference.

decltype((i)) d;//error:dyes int &,Must be initialized
decltype(i) e;//Correct:eIs a (uninitialized) int

Posted by niwa3836 on Mon, 01 Apr 2019 05:57:30 -0700