Http://blog.csdn.net/my_business/article/details/7497248 What is C ++/CX? First, you must understand that it is completely different from C ++ 0x/11 and C ++/CLR. C ++ 0x/11 is the latest C ++ standard library, while C ++/CX is actually Microsoft's Development Platform in Win8, an extension of the C ++ language. C ++/CLR is for the purpose of C ++.. net. It is also an extension of C ++ for CLR, but it is hosted on CLR after compilation and belongs to managed C ++. C ++/CX belongs to native C ++, which neither uses CLR nor has a garbage collection mechanism. Although some new syntax features of c ++/CX are directly referenced from/CLR, they are two different extensions in terms of underlying implementation. This article briefly introduces some syntax extensions of C ++/Cx.
1. Value and reference type ref
Both value and ref are used to define the class, so there is the following syntax:
[CPP] View plaincopy
- ref class object {};
- value class Object {};
what are the differences between the two? In fact, the reference type ref is similar to the reference concept in Java, and the value type value is the c ++ type we generally understand. So why do we need to add such keywords for differentiation? The reason is that both the ref type and the value type have some defects.
value
type value: It is highly efficient to allocate space on the stack, but it does not support polymorphism. For example, when used as a function return value, sometimes there may be some copy problems. In fact, all values in the Standard C ++ are value
type values. Let alone pointers. Pointers are only integer address values. They are still value types, also, don't tell me what function parameters are passed with passing values and passing addresses. Even if the passing address is used, the passed pointer
itself is still passed in the form of values.
the reference type ref supports polymorphism, But it allocates space on the stack, which is less efficient and brings about overhead on the additional reference count.
Therefore, C ++/CX decides to separate the two so that developers can choose based on the actual situation. For example, if there is no need to support polymorphism, we can consider using the value type. For example, if we want to hand it over to the system to manage the reference count, we can consider using the ref type.
2. Public classC ++/CX has the following definition: [CPP]View plaincopy
- PublicRefClassObject {};
- PublicValueClassObject {};
To define a class using public, the function is obvious. All classes to be made public must be set
Public, for example, the class published in the component. If it is not defined as public, the specified type cannot be accessed externally. In addition, native
C ++ classes (classes in Standard C ++) cannot be defined as public. Only classes of the ref and value types can be set to public.CodeAn error is reported during compilation:[CPP]View plaincopy
- Public ClassObject {};// Compiler Error
The following two points are also summarized:
1. The standard C ++ class cannot be used in the public attribute member of the public class.
2. In the public attribute member of the public ref type class, the class member variables cannot be declared unless property is used.
3. In a public attribute member of a class of the public value type, members other than the class member variables, such as functions, cannot be declared.
3. ^ symbol In C ++/CX, ^ is used to indicate that the current data type is the ref reference type. For example, the following class is available:
[CPP] View plaincopy
- RefClassSomeobj {
- Public:
- VoidSomefunc ();
- };
When this class object is created, the object will be created in the following way because it is of the ref reference type:[CPP]View plaincopy
- Someobj ^ OBJ = refNewSomeobj ();
Use "->" for a specific member object of the category class"
[CPP] View plaincopy
- OBJ-> somefunc ();
^ Represents the reference type, and the system will take charge of their reference count. When the reference count is 0, they will be destroyed.
4. Property You can use the property keyword in the public attribute to publish the class member variables, for example:
[CPP] View plaincopy
- RefClassSomeobj {
- Public:
- PropertyIntPropertyx;
- };
You may think that the above writing method is no different from without property. It is actually the same in usage, however, the property method will automatically generate the default get () and set () functions for m_propx, similar to the following: [CPP]View plaincopy
-
- RefClassSomeobj {
-
- Public:
- PropertyIntPropertyx {
-
- IntGet (){ReturnMX ;}
-
- VoidSet (IntX) {MX = x ;};
-
- }
-
- Private:
-
- IntMX;
-
- };
So what is the practical significance of using the property keyword?First, if it is a class defined by public, the member variables can be defined only through property in its public attribute. compilation errors may occur if the following definition is used: [CPP]View plaincopy
- PublicRefClassSomeobj {
- Public:
- IntMX;// Compiler Error
- };
Second, you can use property to control the access permissions of member variables, such as read-only and readable and writable. The following code sets propertyx to read-only: [CPP]View plaincopy
- ref class someobj {
- Public :
- property int propertyx {
- int get () { return MX ;}
- }
- private :
- int MX;
- };
At the same time, we can also define the read or write operations for the member variables. The following code controls the write operation to be set only to a non-0 value: [CPP]View plaincopy
-
- RefClassSomeobj {
-
- Public:
-
- PropertyIntPropertyx {
-
- IntGet (){ReturnMX ;}
- VoidSet (IntX ){
-
- If(X! = 0) MX = X;
-
- };
-
- }
-
- Private:
-
- IntMX;
-
- };
5. Delegate delegate and event Let's first look at an instance of delegate:
[CPP] View plaincopy
-
- DelegateVoidEventhandler (IntX );
-
- RefClassWinrtobj sealed
-
- {
-
- Public:
-
- Winrtobj (){
- Ehandler = refNewEventhandler ([This] (IntX ){
-
- This-> MX = X;
-
- });
-
- }
-
- VoidFireevent (IntX ){
- Ehandler (X );
-
- }
-
- Eventhandler ^ ehandler;
-
- Private:
-
- IntMX;
-
- };
delegate declares the eventhandler function is similar to the function pointer, in In the winrtobj class, we define eventhandler ^ type member variables, which are initialized in the constructor, new the eventhandler parameter is an anonymous function of a Lambda expression, therefore, in fireevent call ehandler (x, in fact, this anonymous function will be run.
we will apply the event to the above example to see what functions the event has: [CPP] View plaincopy
- DelegateVoidEventhandler (IntX );
-
- RefClassWinrtobj sealed
-
- {
-
- Public:
-
- Winrtobj (){
-
- Ehandler + = refNewEventhandler ([This] (IntX ){
- This-> MX = X;
-
- });
-
- }
-
- VoidFireevent (IntX ){
-
- Ehandler (X );
-
- }
-
- Event eventhandler ^ ehandler;
- Private:
-
- IntMX;
-
- };
The above code is just declaringThe event keyword is used when ehandler is created.When ehandler is used, "=" is changed to "+ = ". So What features does event add? We can see from "+ =" that we can use "+ ="Add multiple delegates to the ehandler as follows: [CPP]View plaincopy
-
- Winrtobj (){
- Ehandler + = refNewEventhandler ([This] (IntX ){
-
- This-> MX = X;
-
- });
-
- Ehandler + = refNewEventhandler ([This] (IntX ){
-
- //......
- });
-
- Ehandler: add (refNewEventhandler ([This] (IntX ){
-
- //......
-
- }));
-
- Auto temp = ehandler: add (refNewEventhandler ([This] (IntX ){
-
- //......
-
- }));
-
- Ehandler: Remove (temp );
-
- }
From the code above, we can also use the add method and remove method to delete the bound delegate in addition to "+ =.Now everyone should understandDelegateIntegrationThe specific role of event.
6.Partial classThe partial keyword is also used to modify the class. It allows the class to define the same class in multiple places and will be automatically merged during compilation. For example: [CPP]View plaincopy
-
- Partial refClassN
-
- {
- Public:
-
- IntMethod1 ();
-
- Static IntSdata1;
-
- };
-
- RefClassN
-
- {
-
- Public:
-
- VoidMethod2 ();
- IntMdata2;
-
- };
What is the purpose? In fact, it should be rarely used in actual development code. The reason why partial is required,
Because Metro
Ui interfaces are drawn in XAML, and Vs will pass the XAML into actual code, because these UI codes belong to the same class as the UI logic code developed by itself, so the part of The XAML transition will
It is defined as partial and will be automatically merged during compilation.
Well, there are so many features about C ++/Cx. In fact, there are many new features in addition to these features, such as built-in types, C ++ templates, and enumeration, there are some new extensions for exceptions. In that case, this article is just an example. For more information, see msdn.