I. Types of objects
The type of object determines the size of the object's memory space, the layout of the memory, the range of values that can be stored in memory, and the operations that can be performed on the object, because the type of the object determines what actions can be performed on it, so the const attribute can also be considered part of the object type. Types are also classified as static and dynamic, and for normal objects, static and dynamic types are generally consistent; for pointers and reference types, static and dynamic types can be the same or different, static types are the types declared when pointers and references are defined, and dynamic types are those that are actually bound when the program runs. When static and dynamic types are not the same, there are generally two cases: one refers to a constant pointer and reference bound to a variable, mainly for the write protection of the data, and the second is directed to the descendants of the object, C + + through this mechanism to achieve the object-oriented polymorphism. In the latter case, a superclass in which an inline object in a subclass is inherited by an access modifier allows access to its public member, which means that the public inherits from the parent class's child class at any time to bind to the parent class's pointer and reference (the inherited access modifier makes the public member of the parent class always visible). Protected inherits from the child class of the parent class in its own scope, its own child class, its friend can be bound to a pointer and reference to the parent class (the inherited access modifier makes the public member of the parent class into its own protected member, and thus only its own, subclasses, and friends can access). A subclass of a parent class can be bound to a pointer and reference to the parent class only in its own scope, and the inherited access modifier causes the public member of the parent class to become its own private member, so that only its own friend can access it. At any time, the actions an object can perform are determined only by its static type.
There is also an incomplete type in C + + that is not complete until the complete definition of any type is incomplete, that an incomplete type can be used as a parameter and a return value for a function declaration, can be used as a pointer and reference to define the type, and can be used to declare it to be a static member of a type. However, the type member cannot be accessed through the type pointer and reference, and the type object cannot be defined (therefore, there is no type of nonstatic member of its own type, and its member functions can do anything within it). Because the use of types generally requires a complete definition of the whole, it is usually defined in the header file.
ii. Definition and declaration of objects
in short, the definition of an object is to create an object entity of the specified type for the specified object name, so that the object needs to be allocated storage space and initialized. The declaration of an object, however, informs a scope of the type and object name of an object entity. For global-space variables and normal functions, their definitions must be unique within the global scope, their declarations can occur more than once, but declarations and definitions must be consistent, so their definitions generally appear and appear in only one source file, and the declarations appear in the header file, guaranteeing a unique definition and a consistent declaration. Later, if the definition changes, only one declaration needs to be modified. At the same time, the source file where the definition resides typically contains the header file where the declaration is located, thereby assisting the compiler in checking 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, belong to different entities, and the inline function needs to be inserted into the call point, whose definition needs to be visible at the call, so the const object and the inline function are generally defined in the header file. Object declaration needs to add extern keyword, in order to show that the compiler object definition elsewhere, do not allocate memory space for the object, the object's definition naturally play the role of object declaration, any object declaration is initialized to be considered as the definition of the object. The definition of a function must have a function body, and the function's declaration cannot appear.
Iii. initialization and assignment of objects:
initialization and Assignment in C + + are two completely different concepts, initialization is to provide the parameters created when an object is created, and the assignment is to modify the value of an existing object; In a class type, the two call different functions, the initialization must be done by the constructor, Because the object creates a qualified interface as a constructor. The assignment must be done by the assignment operator, and the constructor cannot be called again for an existing object. Because the const object cannot invoke the assignment operator, it must provide explicit initialization at creation time, the reference type is lifetime bound, mindedness, and therefore must also provide display initialization.
How to initialize in C + +:
Two types of implicit initialization
1) Default initialization:
initialization scenario: A local nonstatic object definition (including an array) is not performed for any display initialization, and the dynamically allocated non-explicitly initialized memory is all initialized by default.
Initialization mode: class type calls default constructor, class type without default constructor is not initialized by default, built-in type undefined
2) Value initialization:
Initialization scenario: no global objects and local static object definitions (including arrays) are displayed for initialization, partially initialized arrays whose remaining uninitialized values are initialized with values.
Initialization mode: class type calls default constructor, class type without default constructor is not value initialized, built-in type initialized to 0 value
three types of explicit initialization
3) Direct initialization:
init Scenario: Explicit use of objname (initializers ...) form is initialized, calling the Emplace function of the container class,make_shared<t> function,make_pair<tk,tv> function, etc., all use direct initialization to create the object.
Initialization method: a constructor that matches an argument directly to the class type, and the built-in type is initialized to the value of initializer
4) Replication initialization:
Initialization Scenario: Displays initialization using objname = Initializer form, normal (non-reference) function arguments, delivery of return values, and so on, using copy initialization.
initialization: The initialization of a copy initialization can only have one, for the class type always calls its copy constructor, copy initialization first creates a temporary object with the specified constructor, and then copies the temporary object to the object being created with the copy constructor. Same for built-in types and direct initialization.
5) Initialization of the list:
init scenario: Show using Objname{initializers ...} Or objname={initializers ...} Initialize, or provide a function argument or return value of {initializers ...} When
initialization: For a class type, it must either have a constructor with a formal parameter of type initializer_list, or it must be simple enough to be encountered in the latter case, and I do not remember any constraints. For built-in types, it is not possible to reduce the precision, that is, the initialization of the int{double} form is illegal.
C + + Summary types, object definitions and declarations, initialization and assignment of objects