Still love C
Stanley B. Lippman
In
glory
Translation
Summary: Programmers using C have been in confusion now: How do their language face C # and Microsoft .NET? This article is taught about how C applies to the road sign of the .NET world. In .NET, C code can be used in two ways: managed and unmanaged. The non-hosting code does not use the CLR, while the managed code is committed to using C hosted extensions. This article discusses these two ways.
The people in our C communities feel that they are like a middle-aged child who has just come to a new baby. Everyone is fanatically surrounded by this new little guy. Even if someone can be light at this moment. Take a picture of our head, we are very fortunate. It is difficult for us to have a feeling of being ignored and a little bit of harm. This is actually worse in technology, because the foundation is constantly drifting, and it is incorporated to survive.
Of course, today's Microsoft .NET framework is new technology that is selling, how rich a big meal! Everyone is in the language called C #. If you are a C programmer, you can't doubt if you should learn C #, after all, in .NET discussion, if you take it with C # contrast, it is rare to mention C .
Is the C programmer out of time? Absolutely not! In this article, I will introduce the new things in the current version of Visual Studio .Net, and give you some Microsoft's ideas about C future plans. First, I will introduce two aspects of C in Visual Studio. N and C hosted extensions.
Maintaining compatibility is the main motivation behind standard C 's latest progress, that is, support for ISO language characteristics. I will also discuss C hosted extensions, which makes C into a .NET language.
Standard C in Visual Studio .NET
Standard compatibility has made progress in the following areas:
l 1. The virtual function definition now supports CoVariant Return Type, which is a good news for people who write class hierarchies.
l 2. Static integer constant members can now be explicitly initialized.
L 3. Added support for hidden in Main.
Below, I will briefly discuss each area.
Join the Candaround Return Type is the first modification of the C language, which has been approved by the Standard Commission. This is a wonderful-derived virtual function now returns a derived entity, and the virtual function in the base class it is returned to an entity of a base class. This is an important design idiom support for class hierarchies, which is also a popular "additive" in Visual Studio .NET.
There is an example of a typical usage. Explain the abstract base class query:
Class query
{
PUBLIC:
Virtual Query * Clone () = 0;
// ...
}
In the entity of the derived Class NOTQUERY, Clone returns a copy of a NotQuery object. E.g:
Class notquery // [Translation: This kind of declaration is a bit problem]
{
PUBLIC:
Virtual ???? clone () {return new notquery (this);
// ...
PUBLIC:
Query * Operand;
}; If there is no support for the class back type, the return type of the NotQuery entity clone must be query *:
// No Coordination Return Type Support ...
Virtual Query * Clone () {return new notquery (this);
This will work very well. If you assign the Return value of Clone to a query * pointer, it is like this:
NOTQUERY :: NOTQUERY (const notquery & rhs) {OPERAND = rhs.Operand-> clone ();
But this is not - when you want to explicitly assign it to a notquery *, as follows:
NOTQUERY NQ (New NameQuery ("Shakespeare"); // [Translation: Less ")"].
// OOPS: illegal assignment ...
NOTQUERY * PNQ0 = nq-> clone ();
// ok: Need to explicitly convert ...
NOTQUERY * PNQ1 = static_cast
With the support of the Coordination Return type, you can explicitly declare that NOTQUERY's Clone returns a NOTQUERY *:
// With Candarings return type support ...
Virtual notquery * clone () {return new notquery (this)
This allows you to use Clone in an intuitive way without explicitting conversion:
// OK: implicit conversion ...
Query * pq = nq-> clone ();
// OK: No need to convert ...
NOTQUERY * PNQ = NQ-> Clone ();
Although the explicit initialization of static integer constant members is not as important as the language characteristics such as Coordination Return, it provides a significant design convenience, such as for fixed size. Array definitions. E.g:
Class buffer
{
Static const INT ms_buf_size = 1024;
Char M_Buffer [MS_BUF_SIZE];
}
Static integer constant members are the only class members that can be explicitly initialized in class definitions in C , so that the value of the member can be used in other ingredients in the class, such as m_buffer dimensions. Otherwise, it is generally replaced by the enumeration as a symbol constant.
Main return value represents the exit status of the program. The discussion, 0 indicates that the program successfully exited. For standard C , Main, there is no explicit return value means that the compiler will insert a line before its tail.
Return 0;
Visual C in Visual Studio .NET finally followed this standard.
Managed C in Visual Studio .NET
The C hosted extension in Visual Studio .NET mainly supports the following three application strategies:
l 1. Provide a managed .NET packaging class for the Non-hosting API, thereby exposing existing C classes to Microsoft .NET platform.
l 2. Allows you to use Microsoft .NET class framework and mix with Unmanaged C . There are three aspects for the framework: core language support, such as collection classes and system I / O; basic programming classes, such as support for threads, network sockets, and regular expressions; application domain support, such as XML, ASP.NET , Web Services, Windows Forms, and ADO.NET, etc.
l 3. You can write directly in the .NET environment, just like it is in C # and Visual Basic. However, this version of C has not yet provided support for RAD designers, such as Windows Forms and Web Forms. First, I will discuss the implementation of packaging a non-hosting. In my "C Primer", I created a relatively large text query application, which focuses on the STL container class to parse text files and establish internal representation. Table 1 shows the included file, Table 2 shows the data representation.
Table 1 Text query application contains files
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
Using namespace std;
Table 2 data representation
Typedef Pair
Typedef vector
Typedef Vector
Typedef Pair
Class TextQuery
{
PUBLIC:
// ...
Private:
Vector
TEXT_LOC * text_locations;
Map
Query * query;
Static string filt_elex;
Vector
}
Query is an abstract base class for interpreting an object-oriented hierarchy of query language. Table 3 shows a possible operation mode of a query session. Text query system a local call looks like the following:
int main ()
{
TextQuery TQ;
tq.build_up_text ();
Tq.Query_text ();
}
Table 3 query session
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,
I hope that there is no need to do what repairs and makeup (more don't tell it), you can expose the TextQuery interface to the .NET platform. After all, it works. I have no bottom in my heart - if I want to move to the .Net platform means I have to give up the effort spent on the local code? Fortunately, the local code is packaged to expose to the .NET platform, which is a good play of Managed C . Table 4 shows the possibility of drying this kind of code. Table 4 Packaging local C class
#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 ();}
// ...
}
__GC modifier (see Table 4) Marking the class as a hosted class - a class that is collected by garbage on the CLR hosted stack. I will declare a local category as a pointer member, using the expression new to configure it on the non-hosting stack, just like I do in the local code. Because it is not collected by garbage, I dropped it in the destructure. BUILD_UP_TEXT and Query_Text are acting as a stub function, and send the call to the actual TextQuery object.
Table 5 shows the modified main function, which is generated by the Managed C project wizard.
Table 5 MAIN function generated
#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;
}
Let us now discuss how to use the .NET framework. If someone asks me that ISO standard C is the most serious shortcoming, I will say that there is a fact that there is no standard library such as thread, network programming, regular expression, and XML. The Standard Commission plans to make up for this shortcomings in the next round, but it is still in this year. What do you do during this? Fortunately, the .NET framework provides an attractive solution. For example, Table 6 shows a simple socket server class that uses the .NET framework. It accepts the phone number query for Northwind's Employees, Northwind is a sample SQL database that distributes with Visual Studio .NET.
Table 6 Simple socket server class
// Contains the necessary assembly (askLIES)
#using
#using
#using
#using
// Open the relevant namespace
Using namespace system; using namespace system :: threading;
Using namespace system :: data;
Using Namespace System :: Data :: Sqlclient;
Using Namespace System :: Collectes;
Using Namespace System :: Net :: Sockets;
// ok: This 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 ();
}
Table 7 is an example output that runs this server and records it handles three client requests. Table 8 shows the implementation of the START function shown in Table 6. The Start function uses the Thread class to generate a standalone thread to get data from the database and process the customer connection. Because WriteLine requires a reference type of parameters, it must be packaged (box).
Table 7 server output
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.
Table 8 START functions
Void SocketDemo_server :: start ()
{
Try
{
TCPL = New TCPListener (port);
TCPL-> Start (); console :: WriteLine (S "Server [{0}]: OK: Started Tcplistener ...", __box (port));
// Net data from the database ...
Thread * TData = New Thread (New ThreadStart (this, & socketdemo_server :: Retrievedata));
TDATA-> Start (); // ok: Pull out the thread ...
// Handle the thread of the 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 ());
}
}
Class Exception is the root of the .NET exception hierarchy. The toString method shows a complete stack trace, which is cool. ThreadStart is a commission type - a generic pointer that can direct static or pointing to non-static member functions.
I can further explore this implementation, but I think you have been able to get a sense of sensibility to the framework and ease of use. More importantly, you can see that it is easyj using C .
C future
I hope that after reading this short text about the C in Visual Studio .NET, you can disappear, heavy tree confidence. Visual C is still one of the members of this family, but it is still an important family member! In order to highlight this importance, the Microsoft Visual C team is working hard to work in a transition version and strive to deliver these features as soon as possible. Talk about ISO Standard C , which has been compatible with exotic steps. For those boat, this means template, template, template. A large number of third-party libraries of the template, such as Loki and Boost, can now be compiled internally without a circle. Just as Hollywood: Don't walk. We are not awkward yet!
- Full text -