Symbol ^

Source: Internet
Author: User
Answer the third question first:

Files containing the Pragma once statement are compiled only once.

The answers to other questions are as follows:

Let's take a look.C ++/How to define the managed type in CLI:

To create an application (reference class) for objects in the managed heap, we can use the following two keywords:

Ref class

Ref struct

Struct means that the access level of its members is public by default, while the class means that the access level of its members is private by default.

If you want to create a value class, you can use the following keywords:

Value class

Value struct

Similarly, for interfaces, the keyword is interface class.

Therefore, we can see the following definition:

Public ref class block {...};

Public Value class vector {...};

Public interface class imyfile {...};

If we need to specify an abstract class, the syntax is:

Public ref class shape abstract {};

Public ref class shape2d Abstract: Public shape {};

You can also specify the sealed type and share sealed and Abstract:

Public ref class string sealed {};

Public ref class State abstract sealed {};

When a hosted object is created, there must be a pointer to it. Here is the first learningC ++/CLI is a bit unaccustomed, because it defines a brand new operator ^.

Let's take a look at some specific examples of the original _ GC * and the new ^, and then feel better:

Public _ GC class form1: Public System: Windows: forms: FORM {

PRIVATE:

System: componentmodel: Container _ GC * components;

Button _ GC * button1;

DataGrid _ GC * mydatagrid;

Dataset _ GC * mydataset;

VoidPrintvalues(Array * myarr)

{

System: collections: ienumerator * myenumerator =

Myarr-> getenumerator ();

Array * localarray = myarr-> copy ();

//...

}

};

Public ref class form1: Public System: Windows: forms: FORM {

PRIVATE:

System: componentmodel: Container ^ components;

Button ^ button1;

DataGrid ^ mydatagrid;

Dataset ^ mydataset;

VoidPrintvalues(Array ^ myarr)

{

System: collections: ienumerator ^ myenumerator =

Myarr-> getenumerator ();

Array ^ localarray = myarr-> copy ();

//...

}

};

Is it more natural?

Some may ask: why not useC ++In the original pointer symbol? Haha, because this is hosting code! If the object is defined on an unmanaged stack, of course, "*" is used. However, if the object is defined on a managed stack, it cannot be simply used. Let's take a look at the following example:

Button ^ button1 = gcnew button; // OK: managed heap

Int * PI1 = new int; // OK: Native heap

Interior_ptr <int32> Pi2 = gcnew int32; // OK: managed heap

Two points are described here:

1. InC ++/In CLI, gcnew is used to indicate that the object is created on the managed stack.

2. For interior_ptr, leave it to be explained later.

At the same timeC ++/In CLI, the number 0 no longer represents a blank address, but only represents a number 0. The empty address has a keyword: nullptr. Therefore, the following code is to send 0 to boxing and then to the pointer OBJ:

Object ^ OBJ = 0;

Only the following statement can actually give a null pointer to the pointer OBJ:

Object ^ OBJ = nullptr;

By the way, let's talk about boxing. Boxing is what happens when you want to treat a value as an object. For example, in the preceding statement, a value 0 is converted into an object. What happens in the background is:

1. First, this value will be pushed into the stack.

2. CLR then pops up the value and allocates a space to store the value and object header information.

3. Then, the reference of a new object will be pushed into the stack.

4. Finally, the reference object is popped up and stored in local variables.

Obviously, the cost is very high. Therefore, do not use boxing (unboxing, of course) unless necessary, because it has a significant impact on performance.

Definition of CLI Array

C ++/In CLI, the array is scheduled:

VoidPrintvalues(Array <object ^> ^ myarr );

VoidPrintvalues(Array <int, 3> ^ myarr );

You can also assign values directly when using gcnew definition:

Array <object ^> ^ myarray =

Gcnew array <object ^> (4) {1, 1, 2, 3}

You can also define a function whose return type is CLI array:

Array <int32> ^ F ();

Array <int> ^ getarray ();
 

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.