C++ notes — variables and primitive types (2)

“This is the second day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”

1. References (rvalue references)

The reference (reference)

2, pointer

Pointer: A compound type that points to another type.

Note:

  • A pointer is a type that typically accounts for 4 bytes for 32-bit programs and 8 bytes for 64-bit programs.

  • The created pointer is itself an object that can be assigned and copied, and without initialization will have an indeterminable value (address).

The definition is as follows:

int x=0;
int *p=&x;
int *p1=new int;
int *p2=new int(32);// Points to an int with a value of 32
int* p3 = new int[5] {1.2.3};// Array of Pointers, allocate 5 int Spaces, p3 points to the header
std::cout << sizeof(p1) <<"" << sizeof(p2) << "" << sizeof(p3) << std::endl;
Copy the code

2.1 pointer value

  • Point to an object;
  • Points to the next location in the space occupied by the adjacent object;
  • A null pointer, that is, it does not point to any object
  • Invalid pointer, other values in the above three cases, such as array out of bounds

Note: Invalid Pointers will cause program execution errors, such as Linux segment errors, but will not cause compilation errors. Attempts to access 2 and 3 will not generate compilation errors, but will cause program errors.

Use Pointers to access objects

The dereference * is used to access objects, such as *p, which together correspond to the object to which P refers. Note: Dereference only applies to valid Pointers

Null pointer

Null Pointers are initialized in three ways:

  • int *p=nullptr; (C++11)
  • int *p2=0;
  • int *p3=NULL;

Note:

int x=0;
int *pi=x;// error, because x is an int, it cannot represent a null pointer, and x is not a pointer
Copy the code

Note: Pointers must be initialized, otherwise an error may cause a crash, which can be very difficult to find

  • Pointers can be assigned and then changed to refer to objects

  • Pointers can be true for judgment conditions, loop conditions, Pointers that are not null, and false for null Pointers

  • Void * a generic pointer that can store the address of any object

2.2 Multiple type composition

int *p1,p2;//p1 is a pointer to int. P2 is int
Copy the code

If you want to define two Pointers

Either:

int *p1,*p2;
Copy the code

Either:

int *p1;
int *p2;
Copy the code

Pointer to pointer, reference to pointer

int **p;//p is a pointer to an int
int *&p;//p is a reference to an int
Copy the code

Note: To understand what p is, read from right to left. The symbol closest to the variable determines its type

Type aliases

  • Tradition: Typedef Type Alias of a type. The asterisk before a type alias follows the type, indicating that the type alias is a pointer to the type
  • C++11 new method: using type alias = type

4, auto

A brief introduction to new C++2.0 features | more challenges in August

5, the decltype

The keyword decltype declares the variable type to the type specified by the expression. For example, decltype(x) y makes y the same type as the new type. Example:

double x;
int n;
decltype(x*n) y;//y is a double
decltype(&x) z;//z is of type double *
decltype((x)) m;//z is of type double &
Copy the code

C++11 adds a syntax for specifying return types after function and parameter names, as shown in this example:

auto double(int x,double y)->decltype(x*y){
    return x*y;
}
Copy the code