Love has a unique clock C ++: Visual C ++ .NET compiler new feature

zhaozj2021-02-17  57

Wang Lingfeng, No. 9 Yingfu, Yingfu, Tianhe Village, Guangzhou

This article assumes that you are familiar with C .

Summary

Old qualifications C programmers want to know: What is the Survival C language in C # and Microsoft? Net impact? This article will make a brief description of C in the world. In .NET, C split into two major camps: Managed Code and unmanaged code. Non-managed code does not use General Language Runtime Environment (CLR), and managed code is used in Managed Extensions for C . This article will be discussed separately.

The feeling of c community is like a big family: the newborn baby is always cared, and the elderly child is unattended. So being ignored, how can I teach people not pain? In fact, the situation in the technical field is worse than this: the technological innovation is too fast, people are tired of running, but they can't help but can't keep up.

Today, Microsoft's new products .NET framework is fried - it is really good; everyone also cheers so-called C # language. As a programmer of C , it is not a taste in his heart: I have to change C #? Because people only have C when they are comparing C # and C .

Is the C programmer going out? no way! This article briefly describes the new features introduced by Visual Studio .NET as C , then introduce Microsoft's plan for new C . First talk about the C : standard C and Managed Extensions for C in Visual Studio .NET from two aspects.

The expansion of standard C is mainly to maintain compatibility; that is, in order to maintain the C language characteristics specified in the International Standardization Organization (ISO). Managed Extensions for C is to incorporate C into the .NET framework.

Standard C under Visual Studio .NET

In order to maintain the compatibility of the ISO standard, standard C has made the following improvements:

(1) The return value of the virtual function now supports the Covariant type, which is a major improvement in the class hierarchy;

(2) Supports explicit initialization of static const integer members;

(3) The return value of the main function defaults to 0.

Now I will introduce them one by one.

The first item is to increase the COVARIANT return type, which has been approved by the Standard Commission. That is, if a virtual function return value of the base class is the instance of the class itself, then it can return the instance of this party itself after being overloaded in the derived class. In the class hierarchy system, this is a very important design pattern; in Visual Studio .NET, it is also popular.

For example, for abstract base classes Query:

Class query {

PUBLIC:

Virtual Query * Clone () = 0;

// ...

}

If you want its derived class instance NOTQUERY, the Clone function returns a NOTQUERY object, namely:

Class notquery {

PUBLIC:

Virtual Query * Clone ()

{return new notquery (this);} // ...

PUBLIC:

Query * Operand;

}

When there is no COVARIANT return type support, Clone's return type is inevitable Query *:

// WITHOUT COVARIANT RETURN TYPE Support

Virtual Query * Clone ()

{RETURN New Notquery

THIS);

At this point, if the return value of the Clone is assigned to the query * type pointer, then everything works fine:

Notquery :: NOTQUERY (Const Notquery & RHS)

{OPERAND = rhs.Operand-> Clone ();

But what if you want to assign it to the NOTQUERY * type?

NOTQUERY NQ (New NameQuery ("Shakespeare");

This will not work:

// OOOPS: ILLEGAL ASSIGNMENT ...

NOTQUERY * PNQ0 = nq-> clone ();

it's the only way:

// OK: Required Explicit Cast ...

NOTQUERY * PNQ1 =

Static_cast (nq-> clone ());

Now there is a COVARIANT return type, you can explicitly return NOTQUERY * type:

// with Covariant Return Type Support

Virtual notquery * clone ()

{RETURN New Notquery (this);

Thus, the return type of Clone becomes more intuitive and no longer need forced type conversion:

This is correct:

// OK: Implicit Conversion ...

Query * pq = nq-> clone ();

This is no problem:

// ok: no conversion necessary ...

NOTQUERY * PNQ = NQ-> Clone ();

The second item is an explicit initialization of the static integer constant type member. It has the impact of C without Covariant's return type. It only brings convenience to the design of classes that use constant expressions (such as: fixed length arrays). E.g:

Class buffer {

Static const INT ms_buf_size = 1024;

Char M_Buffer [MS_BUF_SIZE];

}

After Static Const Int type members are initialized, you can use other members of this class, such as setting the size of the M_Buffer, without using the enumeration variable as before.

The third item is that the default return value of the main function is 0. The return value of the main function represents the exit status of the program. Habits, returning 0 The representative program is ended normally. In standard C , if there is no explicit specified return value, the compiler will automatically insert a line at the end of the main function:

Return 0;

Enter the Visual Studio .NET era, Visual C finally supports it!

Visual Studio .Net under managed C

Visual Studio .NET's Managed Extensions for C provides three basic application policies:

(1) Provide .NET "Packaging" for the original API, transplanted C types into the .NET platform. (2) Mixed C class with three frameworks of Microsoft .NET:

Core language support, such as collected classes and system input / output;

Basic procedures, such as thread support, network socket, and regular expressions;

Application domain support, for example: XML, ASP.NET, Web Services, Windows Forms, ADO.NET, and more.

(3) Operate the .NET environment directly like C # and Visual Basic. However, there is currently no corresponding fast development tool (RAD) for Windows Forms and web form visual programming.

Let's take a look at the first solution: Package the original code with .NET. In my C Primer book (Addison-Wesley, 1998), I demonstrate a considerable text query system, where a large number of STL containers are used to resolve text files and establish a data structure. Its reference file is as follows:

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

Using namespace std;

The data structure created is as follows:

Typedef Pair .

Typedef vector Loc;

Typedef Vector text;

Typedef Pair text_loc;

Class textQuery {

PUBLIC:

// ...

Private:

Vector * lines_of_text;

TEXT_LOC * text_locations;

Map * Word_map;

Query * query;

Static string filt_elex;

Vector line_cnt;

}

The Query class is an abstract base class for explaining the query language, and the features are demonstrated:

ENTER A Query-Please Separate Each Item By a Space.

Terminate Query (or session) with a dot (.).

==> Fiery && (bird || shyly)

Fiery (1) Lines Match

Bird (1) Lines Match

Shyly (1) Lines Match

(bird || shyly) (2) Lines match

Fiery && (bird || shyly) (1) Lines match

Requested Query: Fiery && (Bird || shyly)

(3) Like a Fiery Bird IN FLIGHT. A Beautiful Fiery Bird, He Tells Her,

The main program code is as follows:

int main ()

{

TextQuery tq; tq.build_up_text ();

Tq.Query_text ();

}

Now I submit the TextQuery interface to the .NET platform, did not modify the code, and there is no overwriting function. In the end, everything goes well. If you have to rewrite the code completely to the .NET platform, I may not do it. Fortunately, the managed C provides a way of packaging transplantation. Below is a way of packaging:

#include "textQuery.h"

__GC Class TextQueryNet

{

Private:

TextQuery * pquery;

PUBLIC:

TextQueryNet (): pquery (new textQuery ()) {}

~ TextQueryNet () {delete pquery;

Void query_text () {pquery-> query_text ();

Void build_up_text () {pquery-> build_up_text ();}

// ...

}

Keywords__gc is used to specify managed classes, which accept garbage collector management, occupying universal language runtime (CLR) is piled up. Like the original code, I declare the native class (Native Class) as a member of the pointer, using the keyword NEW in the non-tube heap allocation space, because it does not support garbage collection management, so use the delete recycling space in the destructor . BUILD_UP_TEXT and Query_Text are residual (STUB) functions for calling real TextQuery objects.

As a control, let's take a look at the main function generated by Managed C Project Wizard:

#include "stdafx.h"

#using

#include

Using Namespace System;

#include "gc_textQuery.h"

Int_tmain (void)

{

Console :: WriteLine

S "Beginning Managed Wrapper Test ...");

TextQueryNet * tqn = new textQueryNet ();

TQN-> Build_up_Text ();

TQN-> query_text ();

Console :: WriteLine

S "Ending Managed Wrapper Test ...");

Return 0;

}

Now let's discuss the use of .NET framework. If someone asks me, what is the biggest shortcoming ISO standard C ? My answer is: it does not provide standard libraries that support applications such as threads, network programming, regular expressions, and XML. The Standards Committee has included them into the next round of work plan, but that is a year later. What should we do now? In fact, the .NET framework has provided a fascinating solution. The following demonstrates a simple Socket server class, which uses the .NET framework:

// incrude the nextary assemblies

#using

#using

#using

#using

// Open Up the associated namespaces

Using Namespace System;

Using Namespace System :: Thieading; Using Namespace System :: Data;

Using Namespace System :: Data :: Sqlclient;

Using Namespace System :: Collectes;

Using Namespace System :: Net :: Sockets;

// OK: Here is Our class

__GC Class SocketDemo_Server

{

Private:

Static const Int port = 4554;

Static const Int maxpacket = 128;

TCPListener * TCPL;

DataSet * DS;

DataRowCollection * rows;

PUBLIC:

SocketDemo_server ();

Void start ();

Void Handleconnection ();

// grab the data from the sql database

Void Retrievedata ();

}

Its function is to query the phone number of NortWind staff; the SQL database can be found in the Visual Studio .NET release.

Let's take a look at its processing records for three customers during the run:

Server [4554]: OK: Started Tcplistener ...

Server [4554]: OK: LISTENING for Connections ...

Server [4554]: OK: RETRIEVED SQL Database INFO ...

Server [4554]: OK: a Client Connected ...

Server [4554]: OK: Client Requested Phone # for full

Server [4554]: OK: FIRST Request for Fuller

Server [4554]: Phone Number for Fuller: (206) 555-9482

Server [4554]: OK: a Client Connected ...

Server [4554]: OK: Client Requested Phone # for king

Server [4554]: OK: FIRST Request for King

Server [4554]: Phone Number for King: (71) 555-5598

Server [4554]: OK: a Client Connected ...

Server [4554]: OK: Client Requested Phone # for full

Server [4554]: OK: Cached Request for Fuller

Server [4554]: Phone Number for Fuller: (206) 555-9482

Server [4554]: OK: a Client Connected ...

Server [4554]: OK: Client Requested Phone # for musil

Server [4554]: OK: FIRST Request for Musil

Server [4554]: Phone Number for Musil: Sorry. Cannot Be Found.

The following is the implementation code of the start function:

Void SocketDemo_server ::

Start ()

{

Try

{

TCPL = New TCPListener (port);

TCPL-> Start ();

Console :: WriteLine

S "Server [{0}]: OK: Started Tcplistener ...", __ box (port));

// Retrieve The Data from The Data Base ...

Thread * TDATA =

New Thread (New ThreadStart (this,

& SocketDemo_server :: Retrievedata);

TDATA-> Start (); // ok: kick off the thread ...

// Thread to Handle A Socket CONNECTION ...

Thread * tConnect =

New Thread (New ThreadStart (this,

& SocketDemo_server :: HandleConnection));

TCONNECT-> Start ();

}

Catch (Exception * EX)

{

Console :: WriteLine

S "OOPS: Unable to set up socketdemo_server");

Console :: WriteLine (ex-> toString ());

}

}

We see that the Start function uses the Thread class to generate a lot of threads, respond to each customer's request. In addition, since the WriteLine function needs to reference the parameters, we use __box (port).

Moreover, the Exception class is the base class for all .NET's exception handling class; the TSTRING method is used to display the entire stack (cool); ThreadStart is the delegate type - can point to static member functions, can also point to dynamic General pointer type for member functions.

Of course, its implementation code can also be further improved, but I think you have now realized the power and ease of use of the .NET framework. More importantly, you have seen it, how simple it is in C !

C future

This article is a brief introduction to C under Visual Studio. I hope you will be confident on Visual C because it is not just a part in Visual Studio .Net, but it is an important member. In order to reflect its importance, Microsoft's Visual C Working Group is working hard to work to complete the new generation of Visual C as soon as possible, which will have all new features described herein. In terms of ISO standard C , the working group has taken a big step. For excellent programmers, it means template, template, or template! A large number of third-party libraries such as Loki and Boost are used in tight work. As people say in the movie: "Looking at it, the good play is still behind!"

.a1 {font-size: 1}

转载请注明原文地址:https://www.9cbs.com/read-28576.html

New Post(0)