[C + +] Composite type

Source: Internet
Author: User

Composite type

A composite type is a type defined based on another type: such as a reference and a pointer

A declaration statement consists of a basic data type and a list of declarators immediately following it.

Reference

The reference has a different name for the object, the reference type refers to another type, and the reference type is defined by writing the declarator as &d, where D is the declared variable name

References must be initialized: Typically, when a variable is initialized, the initial value is copied to the newly created object. When a reference is defined, however, the program binds the reference to its initial value, rather than copying the initial value to the reference, and once the initialization is complete, the reference is bound together with its initial value object.

The reference is not an object and there is no actual address. Instead, it is just another name for an already existing object. Therefore, you cannot define a reference reference.

Once a reference is defined, all operations on it are performed on the object to which it is bound.

A reference can only be bound to an object, not to a literal value or to a calculation of an expression. And the referenced type is strictly matched to the object to which it is bound.

Pointer

A pointer is a composite type that points to another type. Indirect access to other objects is implemented.

The pointer itself is an object that allows the pointer to be assigned and copied, and it can point to several different objects in the lifetime of the pointer.

The pointer does not need to assign an initial value when defined. Pointers that are defined within the scope will also have an indeterminate value if they are not initialized.

double dp, *DP2; // DP2 is a pointer to a double-type object, and DP is a double-type object. 

The pointer holds the address of an object, and you need to use the Fetch address (&) to get the address.

int  the ;  int *p = &ival; // P holds the address of the variable ival, or P is a pointer to the variable ival. 

When you define a pointer, the type of the pointer is exactly the same as the object it points to.

You cannot define a pointer to a reference.

Double Dval;  double *PD = &dval; // The initial value is the address of the double type Object  double *pd2 = PD;   // The initial value is a pointer to a double object

The value of the pointer (that is, the address) should be one of the following 4 states:

1, point to an object.

2. Point to the next position in the space occupied by the object.

3, null pointer, means that the pointer does not point to any object.

4. Invalid pointer, which is a value other than the above case.

If the pointer points to an object, the object is allowed to be accessed using the dereference (operator *).

Assigning a value to the result of a dereference is, in effect, assigning a value to the object that the pointer refers to.

Null pointer

A null pointer does not point to any object, and the code can first check whether it is empty before attempting to use a pointer.

The most straightforward way to get a null pointer is to initialize the pointer with literal nullptr. Nullptr is a special type of literal value that can be converted to any other pointer type.

Assign value

The reference itself is not an object, and once a reference is defined, it cannot be bound to another object, and each time it is used, the reference is accessed to the object it was originally bound to.

Pointers, like other non-reference variables, assign a value to a pointer so that it holds a new address, which points to a new object.

The assignment forever changes the object to the left of the equals sign.

Two pointers hold the same address value as there are three possible:

1, they are empty.

2. All points to the same object

3. All points to the next address of the same object.

Note that one pointer points to an object, while another pointer points to the next address of the other object, and it is possible that the two pointers have the same value, that is, the pointer is equal.

void* pointer

Void* is a special type of pointer that can be used to store the address of any object. It is not known exactly what type of object the address is stored in.

The object referred to by the void* pointer cannot be manipulated directly.

In void* 's view, memory space is simply memory space, and there is no way to access the objects stored in the memory space.

Declaration of a composite type

The definition of a variable includes a basic data type and a set of declarators. In the same definition statement, although the base data type has only one, the form of the declarator can be different. The type modifier is part of the declarator.

int  1024x768, *p = &i, &r = i; // I is a number of type int, p is an int type pointer, r is an int type reference

Pointer to pointer

Pointers are objects in memory and have their own addresses like other objects, allowing the address of the pointer to be stored in another pointer.

int 1024x768 ;  int *pi = &ival; // Pi points to a number of type int   int **ppi = π // PPI points to a pointer of type int

A pointer to the int type will get the number of an int, and a pointer to the pointer will get a pointer.

Reference to Pointer

The reference itself is not an object, so a pointer to a reference cannot be defined.

But the pointer is an object, so there is a reference to the pointer

int  the ;  int *p; // p is an int type pointer  int *&r = p; //  R is a reference to the pointer p r = &i; // R refers to a pointer, so assigning r a value &i is to point P to I   0; // The dereference R gets I, which is the object that P points to, changes the value of I to 0

To understand what type of R is, you need to read the definition of R from and to the left. & indicates that R is a reference. * Make sure R refers to a pointer. An int indicates that r refers to an int pointer.

Tips:

1. The pointer itself is an object that allows copies and assignments between pointers, and can point to different objects in its life cycle. and quote No

2. Pointers do not need to be assigned values when defined.

3. The reference must be initialized

4, reference is not an object, no actual address. Instead, it is just another name for an already existing object.

5, void* can receive any type of object

6. Pay attention to the multiple meanings of & and *.

[C + +] Composite type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.