C++/cx easy to get started

Source: Internet
Author: User

C++/CX Introduction

C++/CX is a native development language provided by Microsoft for Windows Store and the Windows Phone Store app developer, which is a extension of C + +, which means that it is compatible with C + +.

As you can see from the name, C++/CX contains C + + and the extension of C + +. The x representation of CX is similar to x in technologies such as DirectX and ActiveX, and represents a variety of technologies in C + + extension.

C++/CX is not only compatible with ISO C + +, but also STL compliant.

For native developers who like C + +, it's helpful to understand some of the c++/cx's understanding of the program in Windows Store app development or the Windows Phone Store app development process.

It is important to emphasize that C++/CX is just a wrapper, this wrapper can be very thin, that is to say, for developers, the majority of the application code can be standard C + +, only a very small part of the C++/CX syntax.

Reasons for using C++/CX and native development

C++/CX can interact with XAML, and can interact with other languages such as C # and VB, while standard C + + is not available. So when we use C + + development, and we need to interact with XAML or other languages, we need to use C++/CX. C++/CX compatible with C + +.

In a Windows development environment, some developers might prefer C #, or Html5+js. Also some like developers like native development, generally have the following reasons

1. Need for more efficient procedures

2. There is an existing C++/C code

3. More familiar with C + + development

4. You need to use DirectX or other APIs that only support C + +. Currently, DirectX development can only use C + + in the Windows Store and the Windows Phone store.

Common C + + 11 syntax

While there are many new features in C + + 11, the following are inevitably encountered when developing Windows Store apps:

1. Auto keyword, as follows: Auto has the function of type deduction here, Obja is automatically judged as the ClassA type.

Auto Obja = new ClassA ();

2. Lamda expression. Java and C # developers may be familiar with this syntax, but many C + + developers are not familiar with it.

3. Null pointer nullptr. In C + + 11, nullptr is a reserved keyword that already exists to represent a null pointer, without requiring a developer macro to define a null representation of a null pointer.

4. Various smart pointers such as Unique_ptr, shared_ptr and weak_ptr

Refer to the following link for the C + + 11 standard

Http://msdn.microsoft.com/en-us/library/hh279654.aspx

Http://www.codeproject.com/Articles/570638/Ten-Cplusplus11-Features-Every-Cplusplus-Developer

Syntax that must be used for C++/CX

1. Ref new. For Windows RT types, you must use ref new to generate the object, as follows. Ref indicates that there is a reference count, that is, C++/CX automatically manages the life cycle of objects by reference counting.

Auto Calendara = ref new Calendar;

Calendara, Settonow (); Invoke with pointer method

2. Ref class for a custom class, if you also want to automatically use reference counting to manage objects, add ref before the class definition as follows:

ref class Classexample {

...

};

3. ^. The representation of a pointer with a reference count. As follows:

Calendar^ Calendarb = ref new Calendar;

Calendarb->settonow ();

(*calendarb).  Settonow (); This is used in the same way as standard C + +, with *

As long as you understand ref and ^, you will be able to read and use the C++/CX code using ISO C + + 11. Check out the unfamiliar places.

Ref pointers Reviews

The two points of ref and ^ should be the biggest changes in c++/cx relative to standard C + +, which is not used by many standard C + + developers when they first see them. Although standard C + + also has smart pointers, the ref pointer in C++/CX has some highlights, such as the size of the ref pointer is the same as the size of a normal pointer (raw pointer), or it can be used with the same cast as standard C + +. As follows. From these two points, C++/cx's pointers seem to be closer to the C + + pointers we learned years ago.

refchild^ Refchild = ref new Refchild ();

refparent^ pparent = Refchild; //Initialize the parent class pointer with the subclass object

Try

{

refchild^ pchild = dynamic_cast<refchild^> (pparent);

Pchild->methodfromchild ();

}

catch (const std::bad_cast& e)

{

Cerr << e.what () << ' \ n ';

Cerr << "This type is not a subclass \ n";

}//Type-safe, natural pointers

C++/CX Reference Counting Principle Example:

{

Auto P1 = ref new Refparent (); the reference count of the//parent object is added to 1, which becomes 1. (Parent AddRef ())

Auto P2 = ref new Refchild (); //child object reference count plus 1, becomes 1 (Child AddRef ())

The parent object reference count is reduced by 1, and the parent object is released. (Parent Release)

Child object reference count plus 1, becomes 2 (Child AddRef ())

P1 = p2;

Reference count of child object minus 1, becomes 1 (Child Release ())

P1 = nullptr;

}//Child Release ()

Say so much first, welcome the exchange of guidance.

C++/cx easy to get started

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.