Research on the attribute of next generation C++:C++/CLI language

Source: Internet
Author: User
Tags list of attributes
Objective:

This article shows the development of the European Computer Developers Association (C++/CLI), a different C + + language, which is convenient for developers at Microsoft. NET Framework easier to develop programs) language in the C + + language extension. The purpose of writing this article is not to suggest that standard C + + includes this part of the expansion, nor is it an endorsement of C++/CLI, but rather a discussion of the direction of C++/cli language in this field.

First, basic knowledge

Attributes in C++/CLI are operational entities similar to the various types of data members (with various operational restrictions), but such operations are often translated into invocation access functions (which are primarily "getter" and "setter" functions). For example:

struct DEMO1 {
property int Val {//A very simple integer, hierarchical attribute.
int get () const {
++demo1::access_count;
Return this->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; Invokes the "Set" action function.
return d.val; Call the "Get" function.
}
The name of the access function must be a get or a set function, either of which can be omitted, but must not be the province of both. Omitting an access function causes only one read property or only one write property to exist. The address of a property cannot be obtained, however, the access function as a member function can legitimately be used to produce a pointer constant to a member (for example: &demo1::val::set).

Properties can be declared using the keyword "virtual", which means that the access action function is a virtual function, and the pure virtual property function can exist, for example:

struct Virtualprop {
Virtual Property int Val = 0 {
int get () const; Pure virtual functions.
virtual void set (int v); Pure virtual function, where the keyword "virtual" is superfluous.
}
// ...
};
The above example shows some simple, non-static, hierarchical property instances that are commonly encountered. The C++/CLI document contains a number of conceptual changes, which are explained below.

Second, the motive

In the context of standard C + +, the property contract moralize the "Get and set function" grammar, which translates the exposed data harmoniously into closed state information. In the context of a finer real-time framework (specifically Microsoft's. NET Framework), attributes are elements that can be detected and modified in real time by mapping. For example, a modern GUI library declares its component parameters as attributes. The visual interface Builder loads these libraries, uses a list of attributes that load various components, and displays the results to the user, and when the user modifies a property, the Access action function is invoked, for example, which triggers various GUI update events.

Third, attribute variables

In addition to the simple hierarchical attributes declared in the above code, C++/CLI also introduces several other types of attribute variables.

(i) Static layered properties

Static layered properties are declared by using the keyword "static", their access operation functions are static, and the access operations of static properties are very consistent with the access operations of static data members. (For example: use C::P syntax to get static property P of Class C)

(ii) an inconspicuous hierarchical attribute

The definition of a property (that is, the declaration of the Access action function in parentheses) can be substituted by the semicolon ";", in which case the get and set accessor functions are combined into a simple property 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:

struct Trivialprop {
Property int Val {
int get () const {return this->__val;}
void set (int v) {this->__val = V;}
}
Private
int __val;
};
(iii) Specify index properties

Using the old syntax of an action array member, the specified index can manipulate a numeric set, and the following example shows the operation of the one-dimensional indexed property.

struct Demo2 {
property int x[std::string] {
int get (std::string s) const {...}
void set (int V, std::string s) {...}
}
// ...
};
int main () {
Demo2 D;
std::string s ("CLI");
D.x[s] = 3; Calls Demo2::x::set (3, s)
return D.x[s]; Calls Demo2::x::get (s)
}
Note that the property of the specified index cannot be a static variable.

Multidimensional indexed properties are also possible, and the operational syntax introduced is not the same as the method of manipulating array elements in C + +, 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) {...}
}
// ...
};
int main () {
Demo3 D;
std::string s ("CLI");
D.x[s, 7] = 42.0; Calls Demo3::x::set (42.0, S, 7)
Return D.x[s, 7]!= 42.0; Calls Demo3::x::get (S, 7)
}
The following example illustrates that the comma symbol for an Action index property that appears in parentheses is an expression action symbol, not a comma operator. (The consequences of this rule are discussed below).

(iv) Default indexed properties

In addition to the object being codified, the default indexed property is very much like the specified indexed property, and the object itself can be indexed (as if it had a [] action member function), and the previous code can explain the change with a slight change.

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) {...}
}
// ...
};
int main () {
Demo4 D;
std::string s ("CLI");
D[s, 7] = 42.0; Calls Demo4::d efault::set (42.0, S, 7)
Return D[s, 7]!= 42.0; Calls Demo4::d efault::get (S, 7)
}
Please pay attention to the keyword "default" in place of the property name.

Four, some technical issues

The European Computer Manufacturers Association (C++/CLI) has studied and addressed several issues associated with introducing attributes, which are particularly noteworthy.

(i) the operation of a multidimensional Index property

P->x[2, 3] expression has different meanings, depending on whether member X is a property (in which case commas separate two indexed properties) or other member variables (in which case the comma is an action symbol). The meaning of an expression is equivalent to p->x[3]). In order to get the effect of the comma operator in a property index, the developer can use parentheses (i.e. p->x[(2, 3)]).

(Note that this is ambiguous in a dependency template expression, and that the problem cannot be resolved until instantiated) the

(ii) property name conflicts with the type name

Microsoft. NET Framework contains a number of classes that contain attributes (which were not originally developed using C++/CLI) that contain the same property names as the property type, for example:

typedef int COLOR;
struct Conflict {
Property color Color {//property name hides type name
TypeName Color get () const;
void Set (typename Color);
}
// ...
};
}
To help write code in this context, C++/CLI plans to add syntax, using keyword typename to identify non-standard types (especially "properties"), and to look for markers that will be ignored. The above code uses the TypeName keyword in this new form.

(iii) overloaded indexed properties

Indexed properties can be overloaded, that is, several specified indexed properties can coexist in the same class using the same name, assuming they can be distinguished by the type of attribute. Similarly, the default indexed properties can be overloaded with other properties or operators []. The rules to resolve both the two-meaning and the overloaded Act have been established to deal with the situation.

(iv) reserved names of members

The C++/CLI attribute is implemented by synthesizing specific members whose names are made by Microsoft. NET Framework, and must be retained.

If a class contains hierarchical attributes or specifies index property X, member names get_x and set_x are preserved in the class (even if the property contains only one action function). Similarly, if a class contains a default indexed property, the member functions get_item and Set_item in the class will also be preserved.

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.