C ++ summary type, object definition and declaration, object initialization and assignment, initialization and assignment
1. Object Type
The object type determines the size of the memory space occupied by the object, the layout of the memory, the range of stored values in the memory, and the operations that can be performed on the object, because the object type determines the operations that can be performed on it, the const attribute can also be seen as an integral part of the object type. Types are classified into static and dynamic types. For common objects, static and dynamic types are generally the same. For pointers and reference types, static and dynamic types can be the same or different, the static type is the type declared during pointer and reference definition, and the dynamic type is the type actually bound when the program is running. When the static and dynamic types are different, there are generally two situations: first, the pointer and reference related to a constant are bound with a variable, which is mainly used to write protection for data; second, it directly refers to the Child class objects. C ++ uses this mechanism to realize polymorphism in the object-oriented model. In the latter case, the nested object of the superclass In the subclass can be bound only when the inherited access modifier allows access to its public member, that is, the public class inherits from the parent class subclass and can be bound to the pointer and reference of the parent class at any time (the inherited access modifier makes the public Member of the parent class visible forever ), protected inherits the subclass of the parent class in its own scope, its own subclass, its friends can bind pointers and references to the parent class. (The inherited access modifier changes the public Member of the parent class into its own protected member, so it only has its own, subclass, and youyuan can access ). The subclass of private inherited from the parent class can only be in its own scope, the member can be bound to the pointer and reference of the parent class. (The inherited access modifier changes the public Member of the parent class into its own private member. Therefore, only the member itself can be accessed. At any time, the operations that an object can perform are only determined by its static type.
There are still incomplete types in C ++. Before the complete definition of any types is completed, they all belong to incomplete types. Incomplete types can be used as parameters and return values of function declaration, it can be used to define the type pointer and reference and can be used to declare it as a static member of a certain type. However, you cannot access members of this type through this type pointer or reference, or define this type of object (therefore, there is no type of nonstatic member of its own type, its member functions can do anything internally ). Generally, all types must be defined in the header file.
Ii. Definition and declaration of Objects
In short, the definition of an object is to create an object of the specified type for the specified object name. Therefore, you need to allocate storage space for the object and initialize it. The object declaration informs a certain scope of the object type and Object Name. For variables in a global space and common functions, their definitions must be unique within the global scope. Their declarations can appear multiple times, but their declarations and definitions must be consistent, therefore, the definition usually appears in only one source file, while the declaration appears in the header file, which ensures the unique definition and consistent declaration. If the definition changes in the future, you only need to modify one declaration. At the same time, the source file where the definition is located should generally contain the header file where the declaration is located, so that the compiler can check the consistency of the definition and declaration. Because constants in the global space have file scopes by default, different source files can have constants of the same name, which belong to different entities. The inline function needs to be inserted to the call point and its definition must be visible at the call, therefore, the const object and inline function are generally defined in the header file. The extern keyword must be added to object declaration to show that the compiler object definition is elsewhere. do not allocate memory space to the object. The definition of the object naturally plays the role of object declaration, any object declaration is considered as the definition of an object once initialized. Function definitions must have function bodies, while function declarations cannot.
3. object initialization and assignment:
In C ++, initialization and assignment are two completely different concepts. Initialization provides the parameters it creates when an object is created, the value assignment is to modify the value of an existing object. In the class type, the two call different functions, and the initialization must be performed through the constructor, because the restricted interface created by the object is the constructor. The value assignment must be carried out through the value assignment operator, and the constructor cannot be called again for an existing object. Since the const object cannot call the value assignment operator, Explicit initialization must be provided during creation. The reference type belongs to the lifetime binding, and must end with a display initialization.
Method of initialization in C ++:
Two types of implicit Initialization
1) default initialization:
Initialization scenario: Partial nonstatic object definitions (including arrays) that are not displayed and initialized are not displayed. The dynamically allocated unexplicitly initialized memory is all initialized by default.
Initialization Method: The class type calls the default constructor. The class type without the default constructor cannot be initialized by default, and the built-in type is not defined.
2) value initialization:
Initialization scenario: the initialization of global objects and local static object definitions (including arrays) are not displayed, and the remaining uninitialized values of some initialized arrays are initialized with values.
Initialization Method: The class type calls the default constructor. Class types without default constructor cannot be initialized. the built-in class initialization is 0.
Three Explicit initialization Methods
3) Direct initialization:
Initialization scenario: explicitly use ObjName (Initializers ...) initialization, calling the emplace hitting function of the container class, make_shared <T> function, make_pair <Tk, TV> function, and so on all use direct initialization to create objects.
Initialization Method: the built-in class
4) replication initialization:
Initialization scenario: displays initialization in the form of ObjName = Initializer. Common (non-referenced) function parameters and transfer of return values all adopt replication initialization.
Initialization Method: Only one initialization object can be copied. For the class type, the replication constructor is always called. during replication initialization, the specified constructor is used to create a temporary object, then, use the copy constructor to copy the temporary object to the object being created. The built-in type and direct initialization are the same.
5) List initialization:
Initialization scenario: When ObjName {Initializers...} Or ObjName = {Initializers...} is used for initialization, or the provided function real parameter or return value is {Initializers ...},
Initialization Method: For a class type, either it must have a constructor with the form parameter initializer_list type, or it must be simple enough. In the latter case, I do not often encounter any constraints. For built-in types, precision cannot be reduced, that is, initialization in the int {double} format is invalid.