Beyond C + +: Introduction to Next Generation C++--C++/CLI

Source: Internet
Author: User
Tags continue reference
First, the introduction

C + + programmers react differently when Microsoft introduced Vs.net7 to implement extensible managed C + +. Although most programmers are relieved to be able to continue using C + +, almost everyone suffers from the arcane syntax provided by managed C + +. Microsoft clearly sensed from feedback that managed C + + was not so successful.

October 6, 2003, ECMA (European Computer Manufacturers Association) announced the formation of a group of experts responsible for the integration of ISO standard C + + and the common language, the development of an extensible language standards, the new extensible language is called the C++/CLI standard. This standard will be supported by the vs.net2005 C + + compiler.

The problems existing in the old grammar

1, the obscure grammar and grammar-these two "double bottom line" problem increased the burden of reading.

2. Second-rate CLI Support-provides CLI support relative to the use of inconvenient workspaces with C # and vb.net,mc++, for example, it does not have a one by one corresponding structure to enumerate. NET of the collection.

3, C + + and. NET rough combination-for CLI types, you can not use the characteristics of C + +, such as templates; Similarly, for C + + types, you cannot use the features of the CLI, such as a fragment collection.

4, confusing pointers--unmanaged C + + pointers and managed reference pointers all use the * syntax, which is confusing because the-GC pointer is fundamentally different from the managed pointer in essence and behavior.

5, the MFC compiler can not produce verifiable code.
 
What does C++/CLI offer us?

1, elegant and fluent grammar and grammar--c++/cli provides a very natural feeling for C + + developers to write managed code, and it provides an excessive smoothness of unmanaged code to managed code. The previously so-called "double bottom line" problem has now vanished.

2. The first class CLI supports--CLI features such as attributes, fragmentation sets, and generic classes that are directly supported, and C++/CLI also allows these features to be used for local unmanaged classes.

3. Class C + + classes support--c++ features such as templates and destructors continue to be effective for drag and drop tube classes. In fact, C++/CLI is one that you can "ostensibly" declare on a stack or C + + local heap. NET-type unique. NET language.

4. In the gully between. NET and C + + A bridge--c++ developers are attacking BCL no longer like fish leaving water.

5, the C++/CLI compiler produces the executable file is completely verifiable.

Four, "Hello World" small program

using namespace System;
void _tmain ()
{
Console::WriteLine ("Hello World");
}
In addition to not having to refer to the mscorlib.dll library, the above code is not much different from the old syntax, because whenever you edit with/clr, the compiler can implicitly refer to it (now the default is/clr:newsyntax).

Five, handle

The main confusion with the old grammar is that we are accustomed to using the * notation to declare a drag-or-drop pointer, and in C++/CLI Microsoft introduced the concept of a handle.
void _tmain ()
{
The ^ Punctuator represents a handle
string^ str = "Hello world";
Console::WriteLine (str);
}

The ^ symbol represents a managed object (the declaration looks like a hat), and the handle represents a reference to a drag-and-drop object, as specified by the CLI. The handle is the new syntax in the CLI, equivalent to the-GC pointer in C + +. The handle is no longer confused with the pointer, which in essence is completely different.

What is the difference between a handle and a pointer?

1. Use the * symbol when the pointer is declared, while the handle uses the ^ symbol.

2. A handle is a drag-and-drop reference to an object on the tow-tube heap, and the pointer only points to an address in memory.

3, the pointer is very stable, the GC loop does not affect it; a handle can point to a different memory location, based on GC or memory tension.

4, for pointers, program developers must "explicitly" delete, or face the risk of leakage, and for the handle, whether the explicit deletion is entirely based on the interests of the program staff.

5, the handle must point to a specific type, that is, the so-called type security, and the pointer is obviously not so, you can never point a handle to the void^ type.

6, just as the new operator returns a pointer, Gcnew returns a handle.

Vii. examples of CLR objects

void _tmain ()
{
string^ str = gcnew String ("Hello World");
object^ O1 = gcnew Object ();
Console::WriteLine (str);
}
Keyword gcnew is used to instantiate a CLI object, and it returns a handle to an object on the CLR heap, the gcnew advantage of which is that it makes it easy to distinguish between drag-and-drop instance objects.

In most cases, the gcnew keyword and ^ operators provide all the means you use to perform BCL, but it is clear that you need to create and declare your own tow-pipe classes and interfaces.

Related Article

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.