C++primer Chapter II Reading notes---variables and basic types

Source: Internet
Author: User

What is a composite type in C + +?

At first, you thought your custom class was a composite type. I checked the C + + primer to know that the composite type is not a class.

In C + +, the type can be broadly divided into three types

First, built-in type

such as int, char, float, unsigned and so on. Built-in types are the most basic types.

Second, compound type

Composite types: Types defined by other types, using types defined by other types. There are three kinds of composite types: reference, pointer, array.

Iii. Types of Classes

is the class. such as string and the class you define.


(i) Composite type---Reference

A reference is a name for an object.

Reference usage Considerations:

1. multiple aliases for a variable

2. The reference must be initialized

3. References can only be referenced once during initialization and cannot be changed to another variable.

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7E/75/wKiom1cAi2PjaD3eAAAezj2aC5I941.png "title=" capture. PNG "alt=" Wkiom1cai2pjad3eaaaezj2ac5i941.png "/>

When a variable is normally initialized, the initial value is copied to the newly created object, but when the reference is defined, it is bound together by the reference and its initial value, and once the initialization is complete, the reference and its initial value are bound all the time because the reference cannot be bound to another object. Therefore the reference must be mindedness.

(ii) composite type---pointers


Pointers are compound types that point to another type, similar to references, and pointers also implement access to other objects, but pointers and references have many different points, such as:

1. A reference can only be initialized once at the time of definition, and then cannot be changed to point to other variables (mindedness), pointer variables

The value of the variable.

2. The reference must point to a valid variable (the reference must be initialized) and the pointer can be empty.

3. The sizeof pointer object and the Reference object do not have the same meaning. sizeof refers to the variable that is being pointed to,

The sizeof pointer is the object address.

4. Pointers and citations (+ +) minus (--) have different meanings.

5. Relative pointers, references are more secure.


Gets the address of the object

The pointer gets the address of an object and wants to get the address, using the Fetch address operator

int ival=42;

int *p=&ival;//p holds the address of the ival, or a pointer to Ival

A pointer to a reference cannot be defined because the reference is not an object and there is no actual address

Recommended initialization of all pointers

Accessing an uninitialized pointer can cause a program to crash, and once it crashes, it can be tricky to locate the wrong location.

Most compilers, if an uninitialized pointer is used, the current contents of the memory space occupied by the pointer are treated as an address value, and access to the pointer is equivalent to accessing a nonexistent object in a location that does not exist.

So...... To be a qualified program ape, we should initialize all pointers and, where possible, wait until the object is defined and then define a pointer to it, and if it is not clear where the pointer should point, simply empty it.


(c) Const qualifier

Sometimes we define a variable, and its value cannot be changed. For example, a variable is used to represent the size of a buffer, and the advantage of using a variable is that it can be adjusted if we think the buffer size is not appropriate, and on the other hand it is feared that the program will accidentally change the value, so it is qualified with the const keyword modifier.

By default, const objects are only valid within a file


Defines a variable when it is initialized at compile time

const int i=2;

The compiler replaces the place where I was used in the compilation with the corresponding value, in order to perform the above substitution, the compiler must know the initial value of the variable, and if the program contains multiple files, each const object's file must have access to its initial value. To do this, it must be defined in each file used in the variable, in order to support this usage, while avoiding the same variable being repeatedly defined, by default the const object is set to be valid in this file, and when multiple files appear with the same name as a const object, they are the same as different variables in different files. , even if the name is the same.

Const reference

Allows you to convert a reference of a very constant type to a reference of a const type, as well as a pointer

int i=10;

Const int&j=i;

Top-Level const

Pointers are objects and references are not

Allow the pointer itself to be defined as a constant, the constant pointer must be initialized, and once the initialization is complete, its value (the address stored in the pointer) can no longer be changed, the top-level const means that the pointer itself is a constant, the underlying const means that the pointer to the object is a constant.

The top-level const means that any object is a constant, which applies to any data type (arithmetic type, class, pointer, etc.), and the underlying const is related to conforming types such as pointers and references

int i=0;

Int*const p1=&i;//cannot change the value of P1, which is a top-level const

The const int q=342;//cannot change the value of Q, which is a top-level const

The const int* p2=&ci;//allows changing the value of the P2, which is an underlying const

C++primer Chapter II reading notes---Variables and basic types

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.