Some casual ideas about the design philosophy of the C ++ language system (to be continued)

Source: Internet
Author: User
Tags define function

Some casual ideas about the design philosophy of the C ++ language system (to be continued)
For a static language, its essential goal is to properly operate the data and obtain the expected value. Specifically, you need to: (1) define the data type, whether it is integer, floating point, or character. What is the range of values that can be included in data of this type. (2) define the meaning of an operation. operations are strictly related to data types. The operation shows the results of a specific type of data. ========================================================== = C ++ is a typical static language. In C ++, both "data type" and "operation" are built-in and custom. The built-in data types of C ++ include: (1) Basic built-in type shaping, floating point, Boolean, and character .... (2) types defined by STL library, such as commonly used iostream, string, iterator ...... in addition, C ++ and define a composite type mechanism, including all types of references, pointers, and arrays, which can be part of a complete data type. By the way, top-level/bottom-level const, static, volatile... modifiers define other attributes of data, which can also be an integral part of a complete data type. The most common custom types are class, struct, union, and function signature. You can also use the composite type mechanism to define reference, pointer, and array of your own classes. ================================ The focus is on whether it is a variable or a constant, must belong to a specific data type. Because operations are defined only based on precise data types ("Semantics" in compilation principles "). That is to say, in a definite set of operations (for example, all operations built into the C ++ language), you only need to assign a variable to the data type, the operations that can be performed by this variable are also confirmed. Defines the variable nVal as int type, then nVal can participate in addition, subtraction, multiplication, division, relational operation, copy, convert to double, pass to function parameters, as the subscript of the array ......... C ++ has a wide range of meanings. In fact, the C ++ language has passed the implicit type conversion defined by member functions, Operator overloading, function overloading, and constructor... and other mechanisms, indicating the nature of C ++ as a static type language: belongs to a specific type of data, plus its operations. It can be understood that the essence of any operation is a function, operators are also viewed as functions in the C ++ language (this can also explain the motivation for C ++ to provide operator Overloading mechanisms); member functions and friend functions of classes, it is also an operation on the "Data Type" of the class itself. Furthermore, operations are a special data type. You can define function pointers, function arrays, and member access (-> *,. *), but there are not many opportunities to use them as data types, and they are also limited by the language itself. C ++'s built-in operations are not easy to understand. In fact, our commonly used language mechanisms are "operations", including: (1) various operator Arithmetic Operators, Relational operators, bitwise operations, address fetch, single-object operations, unreferencing, array element access ..... (2) copy operation copy initialization, list initialization (C ++ 11), value assignment, function passing parameters, function return values, and temporary variable copying for type conversion ...... other non-reference scenarios (3) data type conversion is also an operation. For common operations, you must match the data type before execution. In reality, it is impossible to always ensure that data with strict type matching is provided in the Code. Therefore, type conversion is also a very common operation in the C ++ language. How can we understand this operation? For example, 1234567int nVal = 42; double fVal = 3.14; double fValTwo; fValTwo = fVal + nVal; // increase the nVal type to double. The addition operation of the last line of the code above will increase the execution type. From the perspective of the compiler, an anonymous variable is generated at this time. The variable class is the same as the type (double) to be matched, and then the int to double type conversion operation is executed, the operation result is saved in this anonymous variable. Then the "+" operation is executed. That is to say, if an operation is selected, a number of fully matched data types are expected. To meet this condition, the system performs type conversion. For the value assignment operation, this operation will expect the Data Type of the = right operand to exactly match the data type on the left side. In this case, it will also be the same as the above, generate anonymous variables, and perform type conversion. After the preparation is complete, execute the "=" operation. Function calling is also based on the same principle, that is, the matching of the real parameter type and the form parameter type.... C ++ internally defines an unusually complex type conversion rule (operation), but most of them are transparent to users. For example, integer lifting-char, short, and bool will first be converted to int; type lifting-preventing loss of precision; Type reduction-loss of precision, common in copy operations. The copy operation strictly matches the source object with the target object, so there is no "integer elevation" in the arithmetic operation ". The copy operation includes copy initialization, value assignment, and function call. All non-bool values assigned to the form parameter can be converted to bool. On the contrary, the copy operation is converted to 0/1. Any new class pointer can be converted to void *; if the array is not used for decltype, sizeof, typeid, and address fetch, it is automatically converted to a pointer pointing to the first element. Conversion from non-underlying const to underlying const-reference to constants and pointers can be bound to constants, which is different from the improvement and reduction of built-in types, the conversion from the underlying const to the non-underlying const is invalid. The conversion from the subclass to the base class-the base class pointer/reference can point to the subclass, which is the basis of polymorphism. Like the underlying const, the opposite conversion is illegal. ...... -PS: one-way conversion between the underlying const and inheritance systems: essentially, the smaller the set of operations that can be performed for a data type, the data can reference/bind a wider range of object types. For example, for data type A, you can perform 26 operations in operA-operZ. Data class new B, the operation that can be executed is A subset of A, such as operH-operN. Then, B's reference/pointer can be bound to A (B's reference/pointer can accept A/A's pointer assignment). On the contrary, it is invalid. Const int * cannot modify the int to which it points, but int * can. That is to say, the Operation Range of the Data Type const int * is smaller than that of int, therefore, the const int * can be bound to the object pointed to by the int * (essentially, the const int * can accept the int * value ). In the inheritance system, the operation scope of the base class must be smaller than that of the subclass, so the base class Pointer Points to/the base class reference subclass is legal. The reason for all this is that for a static type language, the compiler always "stubborn" and "self-righteous" determines whether an operation is legal based on its static declaration type, regardless of the type to which the object actually points. As you can imagine, the compiler thinks that int * can change this int, regardless of whether the int * actually points to the const int. If the underlying const can be converted to a non-underlying const, this will bring about conflicts. -PS: to understand the essence of overload function overloading and operator overloading, multiple operations are defined using the same name. The result is that a process is introduced in the compilation phase to determine the specific operation-select the most matched operation from the candidate operation. The preceding "type conversion" operation is performed in the running stage.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.