Arrays

An array is a collection of objects with a certain sequential relationship, and the objects that make up the array become elements of that array. An array name is a pointer that cannot be assigned, called a pointer constant.

Two-dimensional arrays in C++ are treated as arrays of one-dimensional arrays. Therefore, two-dimensional arrays are stored in row-major order.


When passing arrays as parameters, the size of the first dimension is generally not specified, and even if specified, it will be ignored.

Program Example—Linear Fitting

View source code


Pointers

Basic definitions and usage are omitted.

Use of void Pointers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main(){
void voidObject; //Cannot declare variables of void type
void *pv; //Can declare pointers of void type
int i = 5;
pv = &i;
int *pint = static_cast<int *>(pv);
cout << "* pint=" << * pint << endl;
return 0;
}

Running result:
* pint = 5

Void pointers are generally only used when the data type pointed to by the pointer is uncertain.

Use of Null Pointers

Arithmetic operations between different types of pointers or between pointers and non-zero integers are meaningless, but pointer variables can be compared with integer 0. 0 is specifically used to represent a null pointer, which is a pointer that does not point to any valid address.

Null pointers can also be represented using NULL.

NULL is a macro defined in many header files, defined as 0.

Object Pointers

As the name suggests, pointers that point to objects.

When using object pointers, object members can be represented very intuitively. The syntax form is object_pointer_name->member_name, which is equivalent to accessing (*object_pointer_name).member_name.

Earlier in class composition, there was a concept called forward reference declaration, where two classes referenced each other during definition. This problem can be solved using pointers.

1
2
3
4
5
6
7
class A;         //Forward reference declaration
class B{
A *x;
};
class A{
B *y;
}

Declaring a pointer instead of an object is allowed. Once A is defined, A objects can be declared.

this Pointer

The this pointer is a special pointer implicitly present in every non-static member function of a class (including constructors and destructors), used to point to the object being operated on by the member function

The this pointer is an implicit parameter of member functions, allowing the function to know which object it’s operating on, thus accessing its data members. For constant member functions, this implicit parameter is of constant pointer type.

Pointers to Non-static Members of Classes

Declaration statements:

1
2
type_specifier class_name::* pointer_name                           //Declare pointer to data member
type_specifier (class_name::* pointer_name)(parameter_list); //Declare pointer to function member

The general syntax for assigning values to pointers to data members is:

1
pointer_name = &class_name::data_member_name;

When accessing data members, you can use:

1
2
3
object_name.*class_member_pointer_name
or
object_pointer_name -> *class_member_pointer_name

The general syntax for assigning values to pointers to member functions is:

1
pointer_name = &class_name::function_member_name;

Pointers to Static Members of Classes

Only ordinary pointers need to be used:

1
2
type_specifier  *pointer_name = &class_name::static_data_member
type_specifier (*pointer_name)(formal_parameters) = &class_name::static_member_function

Dynamic Memory Allocation

In C++ programs, two operators are used to create and delete heap objects (storage units applied for and released during program execution): new and delete.

Where there’s new, there must be delete for release, otherwise it leads to inability to reclaim memory, causing the program to occupy increasingly more memory, called memory leakage.

Creating an Object of a Class

Details to note when new creates an object of a class.

If the class has a user-defined default constructor, then new T and new T() have no difference.

If the class does not define a default constructor, using new T will call the system-generated implicit default constructor; using new T() will, in addition to executing this implicit function, also initialize members of basic data types and pointer types with 0, and this process is recursive.

Creating Multi-dimensional Arrays

The form is as follows:

1
new type_name T[array_first_dimension_length][array_second_dimension_length]...;

Where the first dimension length of the array can be any expression that results in a positive integer, while the lengths of other array dimensions must be constant expressions that result in positive integers.

Incorrect example:

1
2
float * fp;
fp = new float[10][25][10]

Here the new operation produces a pointer to a $25 \times 10$ two-dimensional float type array, while fp is a pointer to float type data, creating a contradiction!

Creating Array Objects with vector

C++ provides a packaged dynamic array—vector, with various types. vector is not a class, but a class template.

The form for defining dynamic arrays using vector is:

1
vector<element_type> array_object_name(array_length);

All elements of array objects defined by vector will be initialized. If they are basic data types, all elements will be initialized with 0; if they are class types, the class’s default constructor will be called for initialization. So it’s necessary to ensure that classes used as array elements need to have default constructors. Initial values can be specified, but all element initial values must be the same.

Reference Related Supplements

References are commonly used for function parameter lists and function return values. Below are two important concepts related to C++ references that C++ programmers must understand:

Using References as Parameters

Using References as Return Values

Deep Copy vs Shallow Copy

The implicit copy constructor only implements shallow copy, but such copying is not suitable for all situations.

Shallow Copy Code Example

Deep Copy Code Example

Strings

C++ has encapsulated a series of complex string operations, forming the string class for more convenient string manipulation.

Many string operations require the assistance of string processing functions in the cstring header file. Using the string class requires including the string header file.

String Related Explanation

Personal Bank Account Management Program

Functional supplement to the previous management program.

Source code on github