3. Classes and Objects (Part 2)

1. Object initialization list

1) The reason for the object initialization list

(1). Must do:

If we have a class member, which itself is a class or a structure, and this member has only a constructor with parameters, there is no default constructor. At this time, to initialize this class member, you must call the constructor with parameters of this class member,

If there is no initialization list, then he will not be able to complete the first step and will report an error.

 

(2) If there is a const modification in a class member, you must assign a value to const int m when the object is initialized

When a class member contains a const object, or a reference, they must also be initialized through the member initialization list,

Because these two objects are initialized immediately after the declaration, and in the constructor, the assignment to them is done, which is not allowed.

grammar rules

Constructor::Contructor() : m1(v1), m2(v1,v2), m3(v3)

{

    

}

Notice:

The initialization order of member variables is relative to the order of declaration, not the order in the initialization list

The initialization list is executed before the function body of the constructor

 

2. Constructor and destructor calling order and reinforcement

1) When there are member variables in the class that are objects of other classes, the constructors of the member variables are called first, and the calling order is the same as the order of declaration; then the constructors of their own classes are called.
2) The calling order of the destructors and the corresponding constructors reverse order of calls

3) The life cycle of anonymous object Class (1) only exists in this line of code. Calling the constructor in the constructor is to generate an anonymous object, which is a dangerous behavior

 

3. Dynamic creation and release of objects

1) In the process of software development, it is often necessary to dynamically allocate and undo memory space, such as insertion and deletion of nodes in a dynamic linked list. In the C language, the library functions malloc and free are used to allocate and deactivate the memory space. C++ provides simpler and more powerful operators new and delete to replace malloc and free functions.
Note: new and delete are operators, not functions, so the execution is efficient.

2) The objects defined by the class name are static, and the space occupied by the object cannot be released at any time during the running of the program. But sometimes people want to create an object when it needs to be used, cancel it when it is not needed, and release the memory space occupied by it for use by other data. This can improve the utilization of memory space.

3) When the new operation is performed, if the amount of memory is insufficient, the required memory space cannot be opened up. At present, most C++ compilation systems make new return a 0 pointer value. As long as the return value is 0, it can be judged whether the memory allocation is successful.

code usage

int main()
{
	int *p1 = new int(10);
	cout << *p1 << endl;
	delete p1;			//释放指针变量

	int *p2 = new int[5];
	for(int i = 0; i < 5; i++)
		cout << p2[i] << endl;
	delete[] p2;		//释放数组的写法

	return 0;
}

The difference between new and malloc is that malloc does not call the constructor when allocating space for the pointer object, while new calls the constructor.

 

4. Static member variables and static member functions

static member variable

The keyword static can be used to describe the members of a class.
    Static members provide a sharing mechanism for objects of the same
 type. When a member of a class is declared as static, no matter how many objects of this class are created, these objects share the static
 member . Local to the class, it is not an object member, initialized outside the class

static member function

The number of static member functions is prefixed with the keyword static
. Static member functions provide common operations that do not depend on the data structure of the class. It does not have this pointer
. Calling static member functions outside the class uses "class name::" as a qualifier, or calls it through objects
. Problem: In static member functions, ordinary variables cannot be used, only static member variables can be used

 

5. this pointer

1. Member variables and member functions in C++ class objects are stored separately. The four-area memory model in C language is still valid
2. The ordinary member functions of a class in C++ implicitly contain a this pointer to the current object
3. Static member functions and member variables belong to the class
The difference between static member functions and ordinary member functions:
A static member function does not contain a pointer to a concrete object An
ordinary member function contains a pointer to a concrete object

class Test
{
public:
    Test(int a, int b) //---> Test(Test *this, int a, int b)
    {
        this->a = a;
        this-> b = b;
    }

 

6. const modified member function

void Test::print() const
{
	//m_a++;	//const修饰的函数不能修改成员变量(read-only)
	cout << m_a << endl;
}

 

7. Tomomoto

friend function

Friend function is not an internal function of the class, it is a global function, but it can change the private properties
of the class Friend function destroys the encapsulation of the class

Tomomoto Metaclass

If class B is a friend class of class A, all member functions of class B are friend functions of class A. The friend
class is usually designed as an auxiliary class for data operations or message passing between classes

 

summary

  • Classes are usually defined with the keyword class. A class is an encapsulation of data members and member functions. An instance of a class is called an object.
  • The structure type is defined with the keyword struct, which is a data type composed of different types of data.
  •  Class members are determined to access properties by private, protected, and public. A set of public members is called an interface.
  •  Constructors are automatically called when objects are created and initialized. The destructor is called automatically when the object scope ends.
  •  Overloaded constructors and copy constructors provide different initialization methods for creating objects.
  •  Static members are class-local members that provide a mechanism for sharing objects of the same type.
  • Friends are declared with the keyword friend. Friends are an auxiliary means of class operations. Friends of a class can access members of various properties of the class.
  • A linked list is an important dynamic data structure that can create or undo data elements while the program is running.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324483320&siteId=291194637