Property exploration of C ++CLI language

xiaoxiao2021-03-05  31

This article shows the C / CLI of the European Computer Developer Association (a different C language, which facilitates developers more easily develop programs under Microsoft) language in the C language extension. The purpose of writing this article is not to recommend that standard C includes this part of the expansion, nor is the recognition of C / CLI, but only explores the development direction of the C / CLI language in this area. I. The attribute in the Basics C / CLI is a operable entity similar to a variety of data members (having various operational restrictions), but this operation is often converted to the call access function (this is mainly "getter" and "" Setter "function). E.g:

Struct demo1 {Property Int Val {// A very simple integer, hierarchical property. INT get () const { demo1 :: access_count; return1-> value;} void set (int V) { demo1 :: access_count; this-> value = v;}} private: int value; static unsigned Long access_count;}; int main () {DEMO1 D; D.VAL = 3; // Call the "SET" operation function. Return D.val; // Call the "GET" function. }

The name of the access function must be a GET or a SET function, and any of the two can be omitted, but it must not be omitted in both. An access function is omitted results in only one read attribute or only one write property. The address of the attribute is unable to obtain, however, the access function can be used as a member function to produce a pointer constant to the member (eg & demo1 :: val :: set).

Properties can be declared using keyword "Virtual", which is the objective function function, and the pure virtual property function may exist, for example:

Struct VirtualProp {Virtual Property Int Val = 0 {INT GET () const; // pure virtual function. Virtual void set (int V); // pure virtual function, here keyword "Virtual" is redundant. } // ...};

Second, the motivation is in the context of standard C , the attribute is contemplated to use the "GET and SET functions" grammar. This grammatism will turn the exposed data to a closed state information. In a more fine real-time frame context (specifically, Microsoft's .NET framework), attribute is an element that can be found and modified by mapping. For example, modern GUI libraries declare its component parameters as attributes, and the visual interface constructed tools are loaded with these libraries, using the list of properties loaded with various components, exhibiting the result to the user, when the user has modified an attribute, access operation The function will be called, for example, this will trigger a variety of GUI updates events.

Third, the attribute variable except the simple layered attributes declared in the above code, C / CLI also introduces several other types of attribute variables. (1) Static hierarchical properties Static hierarchical properties use keyword "static" to declare that their access operation function is static, and the access operation of the static attribute is very consistent with the access operation of the static data member. (For example, using a C :: P syntax to get a static attribute P) (2) a definition of an attribute of a non-obvious hierarchical property (ie the access operation function declaration within the parentheses) can use the semicolon ";" Instead, in this case, the GET and SET access functions are integrated into a simple attribute value that can be accessed. For example, a class defined by C / CLI is as follows: struct triVialprop {property int val;};

The above code is essentially the same as the following code:

Struct trivialprop {prot {ion THIS -> __ Val;} void set (int V) {this -> __ var = v;}} private: int __val;};

(3) Specify index properties

Using an Operating array member, specify an index to operate a numeric collection, the following example shows a one-dimensional index property.

Struct demo2 {Property Int x [std :: string] {int GET (std :: string s) const {...} void set (intv, std :: string s) {...}} // .. }; int main () {Demo2 D; std :: string s ("CLI"); DX [S] = 3; // Calls Demo2 :: x :: set (3, s) return [s]; // Calls demo2 :: x :: get (s)}

Note that the attributes of the specified index cannot be static variables.

The multi-dimensional index properties are also possible, and the operational syntax introduced is not only the same as the C / C in-array element operation method, for example:

Struct demo3 {Property Double X [std :: string, int] {double get (std :: string s, int n) const {...} void set (double v, std :: string s, int n) {. .. 42.0, S, 7) Return DX [S, 7]! = 42.0; // Calls Demo3 :: x :: get (s, 7)}

The following example illustrates a comma symbol that appears in parentheses in parentheses, is an expression operation symbol, not a comma operator. (The consequences of this rule will be discussed below).

(4) Default index properties

In addition to the object being encapsulated in the pseudo domain, the default index attribute is very similar to the specified index property, the object itself can index (as if itself has a [] operation member function), the previous code can explain this. Variation.

Struct demo4 {Property Double Default [std :: string, int] {Double Get (std :: string s, int N) const {...} void set (Double V, std :: string s, int n) {. .. 42.0, S, 7) Return D [S, 7]! = 42.0; // Calls Demo4 :: Default :: Get (s, 7)} Please pay attention to the use of keyword "default" instead of the property name.

Fourth, some technical issues

The European Computer Manufacturers Association (C / CLI standard settlers) has studied and solve several problems brought about by introduction attributes, and the following is especially worthy of attention.

(1) Operation of multi-dimensional associate properties

P-> x [2, 3] The expression has a different meaning, and this must be regarded as the member X is attribute (this case separated two index properties) or other member variables (in this case is an operation symbol, The expression is equivalent to P-> x [3]). In order to obtain a comma operator in an attribute index, the developer can use parentheses (ie p-> x [(2, 3)]).

(Note, in the expression of the plate, this will generate ambiguity, and the problem can be solved until the instantiation)

(2) Attribute name and type name conflict

Microsoft .NET Framework has a lot of properties

Class (these classes are originally not developed using C / CLI), which are the same as the name of the attribute type, for example:

TypeDef int color; struct conflict {proteth color color {// Property name hides type name type name color get () const; void set (TypenAme color);} // ...};

In order to help write code in this context, C / CLI plans to add syntax, use keyword Typename to identify an unbolded type (especially "properties"), and will be ignored during the finding flag. The above code uses TypenAme keywords in this new form.

(3) Index attributes of overload

Index properties can be overloaded, ie, several specified index properties can be saved in the same class using the same name, assuming that they can distinguish from the type of properties. Similarly, the default index properties can be overloaded using other attributes or operators []. Solving the rules of two-oriented and overload behavior have been established to process the above.

(4) Reserved member name

C / CLI properties are implemented by a comprehensive specific member, and the name of these members is specified by Microsoft's .NET framework and must be reserved.

If a class contains a hierarchical property or specifying an index property X, the member name get_x and set_x are reserved in the class (even if the attribute only includes one operation function). Similarly, if a class contains a default index property, the member function GET_ITEM and SET_ITEM in the class will also be reserved.

转载请注明原文地址:https://www.9cbs.com/read-32487.html

New Post(0)