Win8 Exploration Study Notes (5) syntax extension of winrt

Source: Internet
Author: User

Refer:

Http://msdn.microsoft.com/en-us/library/windows/apps/hh454076 (V = vs.110). aspx

(1) winrt object and reference count

Winrt is an OO library, so it is operated in the form of objects. In winrt, use reference count to manage objects and keywordsRef newCreate an object and return the Object Pointer. However, in winrt, use^Symbol replacement *, still use the-> operator to use the object member method. In addition, because the reference count is used, you do not need to use Delete to release the object. The object is automatically released when the last reference is invalid.

Note: winrt is implemented using COM internally. For reference counting, at the underlying layer, the winrt object is a COM Object managed using smart pointers.

Platform::String^ str1 = "str1";Platform::String^ str2 = str1;Platform::String^ str3 = ref new Platform::String(L"str3");

Note: str1 here is the object's "Pointer", which is different from STD: string.

(2) number type in winrt

bool    b    = true;char    c8   = 'A';char16  c16  = L'A';                // same as wchar_tint8    i8   = 0x12;                // same as charint16   i16  = 0x1234;              // same as shortint32   i32  = 0x12345678;          // same as int, longint64   i64  = 0x1234567890abcdef;  // same as long long, __int64int8    ui8  = 0x12;                // same as unsigned charint16   ui16 = 0x1234;              // same as unsigned shortint32   ui32 = 0x12345678;          // same as unsigned int, unsigned longint64   ui64 = 0x1234567890abcdef;  // same as unsigned long long, __uint64float32 f32 = 12.34E-23;            // same as floatfloat64 f64 = 1234.56E-234;         // same as double, long double

The C ++ numeric type is automatically converted to the corresponding winrt type. For example, a numeric value is returned from a standard C ++ function and assigned to the winrt type.

Note: These types are defined in the platform namespace.

(3) ref class implement custom class

When you mark a custom class with ref class, you can use this class using the winrt object. Of course, some types of Standard C ++ can be used together in classes.

(4) value struct

If a class only contains data members, you can use value struct to define it:

value struct Coordinates{  float64 Latitude;  float64 Longitude;};value struct City{  Platform::String^ Name;  int64 Population;  int Altitude;  Coordinates Coordinates;};

(5) properties in winrt

In winrt, the property keyword is used to declare a total of data members. In addition, you can set the get () and set () logic for the property to access it.

ref class Prescription{private:  Platform::String doctor_;  int quantity_;public:  property Platform::String Name;  property Platform::String Doctor  {    Platform::String get() { return doctor_; }  }  property int Quantity  {    int get() { return quantity_; }    void set(int value)    {      if (value <= 0) { throw ref new Platform::InvalidArgumentException(); }      quantity_ = value;    }  }}

(6) winrt Interface Definition

Winrt can also implement interface functions similar to Standard C ++, of course, the standard C ++ does not provide interfaces (many new languages such as C # and Java have provided interfaces ), however, standard C ++ uses pure virtual classes to implement this function. Winrt uses the interface keyword to define a class as an interface class (only the method needs to be declared but not implemented), and then it can be inherited and implemented by other ref classes.

(7) winrt delegation, events, and partial classes

Winrt also has some other syntax extensions, such as delegation and events. It is found that these extensions are the same as C #, so we will not continue to talk nonsense. For more information, see the reference link.

PS: I thought there would be something special and I found it was all C #'s old stuff.

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.