[Windows 8 Development] C ++/CX)

Source: Internet
Author: User
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
    1. ref class object {};
    2. 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

    1. PublicRefClassObject {};
    2. 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

    1. 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
    1. RefClassSomeobj {
    2. Public:
    3. VoidSomefunc ();
    4. };

 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

    1. Someobj ^ OBJ = refNewSomeobj ();
Use "->" for a specific member object of the category class" [CPP] View plaincopy
    1. 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
    1. RefClassSomeobj {
    2. Public:
    3. PropertyIntPropertyx;
    4. };

 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

  1. RefClassSomeobj {
  2. Public:
  3. PropertyIntPropertyx {
  4. IntGet (){ReturnMX ;}
  5. VoidSet (IntX) {MX = x ;};
  6. }
  7. Private:
  8. IntMX;
  9. };

 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

    1. PublicRefClassSomeobj {
    2. Public:
    3. IntMX;// Compiler Error
    4. };

 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

    1. ref class someobj {
    2. Public :
    3. property int propertyx {
    4. int get () { return MX ;}
    5. }
    6. private :
    7. int MX;
    8. };

 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

  1. RefClassSomeobj {
  2. Public:
  3. PropertyIntPropertyx {
  4. IntGet (){ReturnMX ;}
  5. VoidSet (IntX ){
  6. If(X! = 0) MX = X;
  7. };
  8. }
  9. Private:
  10. IntMX;
  11. };
5. Delegate delegate and event Let's first look at an instance of delegate: [CPP] View plaincopy
  1. DelegateVoidEventhandler (IntX );
  2. RefClassWinrtobj sealed
  3. {
  4. Public:
  5. Winrtobj (){
  6. Ehandler = refNewEventhandler ([This] (IntX ){
  7. This-> MX = X;
  8. });
  9. }
  10. VoidFireevent (IntX ){
  11. Ehandler (X );
  12. }
  13. Eventhandler ^ ehandler;
  14. Private:
  15. IntMX;
  16. };

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

  1. DelegateVoidEventhandler (IntX );
  2. RefClassWinrtobj sealed
  3. {
  4. Public:
  5. Winrtobj (){
  6. Ehandler + = refNewEventhandler ([This] (IntX ){
  7. This-> MX = X;
  8. });
  9. }
  10. VoidFireevent (IntX ){
  11. Ehandler (X );
  12. }
  13. Event eventhandler ^ ehandler;
  14. Private:
  15. IntMX;
  16. };

 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

  1. Winrtobj (){
  2. Ehandler + = refNewEventhandler ([This] (IntX ){
  3. This-> MX = X;
  4. });
  5. Ehandler + = refNewEventhandler ([This] (IntX ){
  6. //......
  7. });
  8. Ehandler: add (refNewEventhandler ([This] (IntX ){
  9. //......
  10. }));
  11. Auto temp = ehandler: add (refNewEventhandler ([This] (IntX ){
  12. //......
  13. }));
  14. Ehandler: Remove (temp );
  15. }

 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

  1. Partial refClassN
  2. {
  3. Public:
  4. IntMethod1 ();
  5. Static IntSdata1;
  6. };
  7. RefClassN
  8. {
  9. Public:
  10. VoidMethod2 ();
  11. IntMdata2;
  12. };

 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.

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.