For statically typed languages, the essential goal is to manipulate the data appropriately to get the desired value. Specifically, you need:
(1) Defining data Types
What is the data you define, whether it is shaping or floating point or character. The range of values that the type of data can contain.
(2) Define the meaning of the operation
Operations are strictly data-type related. The operation shows what the result is for a specific type of data that is executed after the operation.
===========================================
C + + is a typical static type language. In C + +, both "data type" and "Operation" are both built-in and customized.
The built-in data types for C + + include:
(1) Basic built-in type
Shaping, floating point, Boolean, character ....
(2) Types of STL library definitions
Examples of commonly used iostream, strings, iterators ...
In addition, C + + and defines a compound type mechanism, including all types of references, pointers, and arrays, which can be part of a complete data type.
By the way, top/Bottom const, static, volatile ... modifiers, which define other properties of the data, can also be part of a complete data type.
For custom Types , the most common is class, struct, union definition, and function signature, and of course you can define your class's references, pointers, arrays, and so on using a compound type mechanism.
==============================
The point is that either the variable or the constant must belong to a particular data type. Because the operation is based only on the exact data type, its definition has a definite meaning (called "Semantics" in the compilation principle). That is, in a certain set of operations (for example, all operations built into the C + + language), as long as a variable is assigned to the data type, the operation that the variable can perform is determined. Define the variable nval to be of type int, then nval can participate in subtraction, relational operation, copy, convert to double, pass to function parameter, subscript as array ....
The "operation" of C + + has a very broad meaning. In fact, the C + + language has been passed through member functions, operator overloading, function overloading, implicit type conversions defined by constructors ... And so on, it shows the essence of C + + as a static type language: It belongs to the specific type of data, plus the operation on it. It can be understood that any operation, essentially a function, is also treated as a function within the C + + language (which also explains the motivation of C + + to provide the operator operator overloading mechanism), the member function of the class, the friend function, and the operation of the "data type" of the class itself.
Further, the operation itself is a special type of data. You can define pointers to functions, arrays of functions, member accesses (->*,.*), but not many opportunities to use as data types, and are limited by the language itself.
The built-in operation of C + + is not very well understood, in fact, our common language mechanism is "operation", specifically contains:
(1) Various operators
Arithmetic operators, relational operators, bitwise operations, FETCH addresses, monocular operations, dereference, array element access .....
(2) copy operation
Copy initialization, list initialization (C + + 11), assignment operation, function parameter, function return value, copy of temporary variables executed by type conversion ... and other non-reference scenarios
(3) Data type conversion
Type conversions are also an operation. For normal operations, the type of data to be manipulated needs to be matched before execution. In reality, it is not always possible to guarantee that the data in the code is strictly matched, so type conversion is also a very common operation of the C + + language.
How do you understand such an operation? As an example, for example:
int nval = 42;double fval = 3.14;double Fvaltwo;fvaltwo = fval + nval; Nval type promoted to double
The add operation of the last line of the preceding code performs type promotion. From the compiler's point of view, an anonymous variable is generated, the class of the variable is the same as the type that needs to be matched (double), and then a type conversion operation of int to Double is performed, and the result of the operation is saved in this anonymous variable. Then the "+" operation is performed. That is, if an operation is selected, the operands are expected to match exactly the number of data types, and in order to satisfy this condition, the system performs a type conversion.
For an assignment operation, the operation expects the data type of the right operand to be exactly the same as the left, and then the same as above, generating an anonymous variable to perform the type conversion. After the preparation is complete, perform the "=" operation.
The invocation of a function is also based on the same principle, that is, the match of the argument type and the formal parameter type.
... ...
The C + + language internally defines an unusually complex type conversion rule (operation), but most of them are transparent to the user. For example:
Plastic Lifting-char, short, bool are converted to int first;
Type promotion-to prevent loss of precision;
Type reduction-loss of precision, common in copy operations. The copy operation is to strictly match the source object to the target object, so there is no "shaping ascension" in arithmetic operations. The copy includes copy initialization, assignment operation, function call argument assignment to formal parameter
Non-bool values can be converted to bool, whereas the other is converted to 0/1;
Any kind of new pointer can be converted to void*;
An array is automatically converted to a pointer to the first element when it is not used for decltype, sizeof, typeid, and fetch address &.
Conversion of a non-underlying const to the underlying const-a reference and pointer to a constant can be bound to a very good amount, and the conversion of the underlying const to a non-underlying const is illegal , unlike the built-in type's elevation and decrease;
The transformation of a subclass to a base class-a base-class pointer/reference can point to a subclass, which is the basis of polymorphism. as with the underlying const, the opposite conversion is illegal .
... ...
-
PS: The unidirectional nature of the underlying const and inherited system type conversions:
Essentially, the data type of a data, the smaller the set of operations that can be performed, the wider the type of object that the data can reference/bind. For example:
Data type A, you can perform Opera-operz a total of 26 operations. The new data Class B, the operations that can be performed are subsets of a, such as Operh-opern. Then, the reference/pointer of B can be bound to a (B's reference/pointer can accept the A/A pointer Assignment), instead it is illegal.
The const int * Cannot modify the pointed int, while int * can, that is, the data type const INT * is smaller than int *, so the const int * can be bound to the object pointed to by int* (essentially, the const int * can accept int* Assignment).
In the inheritance system, the operation scope of the base class is certainly less than the subclass, so the base class pointer to the/base class refers to the legal of the subclass.
The reason for all this is that for statically typed languages, the compiler always "stubbornly" and "self-righteous" in accordance with its statically declared type, to determine whether an operation is legitimate, rather than the type that the object actually points to. As you can imagine, the compiler is "smug" to think that an int * can change this int, regardless of whether the int* is actually pointing to a const int, which can cause a conflict if the underlying const is allowed to convert to a non-underlying const.
-
PS: Understanding overloading based on this view
The essence of function overloading, operator overloading, is that multiple operations are defined with the same name. The result is the introduction of a process to determine the exact action during the compilation phase-selecting the most matching action from the candidate operation. The above "type conversion" operations are performed at run time.
Custom Actions , including common functions we define, member functions, overloaded operators, implicit type conversions for constructor definitions, copy operations defined by copy constructors ...
Not to be continued
Some random thoughts on the design philosophy of C + + language system (not to be continued)