C++ Classes and Objects (3) Empty Classes and Default Member Functions

Table of contents

1. Empty class

Two, 6 default member functions


1. Empty class

1. What is it?

If a class does not explicitly define any members, it is simply called an empty class.

class A {};//一个空类

2. A member in an empty class?

Is there really nothing in the empty class?

No, when any class does not write anything, the compiler will automatically generate 6 default member functions.

(Default member function: The member function automatically generated by the compiler is called the default member function if the user does not explicitly implement it)


Two, 6 default member functions

Default member function: The member function that is automatically generated by the compiler without being explicitly implemented by the user is called the default member function.

1) Constructor: Initialize the member variables in the object when the object is created.

2) Destructor: Complete the destruction of the object.

3) Copy constructor: With this function, objects of the same type can be initialized to create objects.

4) Assignment overload function: With this function, one object can be assigned to another object.

5) Overloaded function for taking addresses of ordinary objects: mainly for taking addresses of ordinary objects, which is rarely implemented by itself.

6) Overload function for taking address of const object: mainly to take address of const object, which is rarely realized by itself.

 

Guess you like

Origin blog.csdn.net/look_outs/article/details/132045255