From the February 2002 issue of MSDN Magazine
Still in Love with C++
Modern Language Features Enhance the Visual C++ .NET Compiler
|Stanley B. Lippman|
|This article assumes you're familiar with C++|
|Level of Difficulty 1 2 3|
SUMMARY Programmers who have been using C++ for years are wondering where their language is headed with the advent of C# and Microsoft .NET. This article sketches a roadmap of C++ as it is used in the world of .NET. In .NET there are two approaches to C++ code: managed and unmanaged. Unmanaged code doesn't use the CLR, while managed code involves the use of Managed Extensions for C++. This discussion explains both approaches.
|hose of us in the C++ community feel somewhat like the older child in a family when the new baby is brought home for the first time. Everybody goes gaga over the new little tyke, and we're lucky if we even get a pat on the head. It's hard not to feel ignored and a little bit hurt. Actually, it's even worse in technology because the ground is constantly shifting, and keeping abreast is a matter of survival.|
Of course, today the Microsoft® .NET Framework is the new technology on the block—quite a feast at that, and everyone is oohing and aahing about the language called C#. If you're a C++ programmer, it is hard not to wonder if you ought to learn C#. After all, in the .NET discussion there is hardly a mention of C++, except to contrast it with C#.
Are C++ programmers obsolete? Absolutely not! In this article I'll touch on what's new in the current release of Visual Studio® .NET, and then give you some idea of the plans Microsoft has for the future of C++. Primarily, there are two aspects of C++ in Visual Studio .NET that I want to touch on: standard C++ and the Managed Extensions for C++.
The motivation behind the recent progress in standard C++ was largely to maintain compliance; that is, to provide support for the International Organization for Standardization (ISO) language features. I'll also talk about the Managed Extensions for C++ in terms of making C++ a .NET language.
Standard C++ in Visual Studio .NETProgress has been made in standards compliance within the following areas:
Adding the covariant return type was the first modification of the C++ language approved by the standards committee. This is a fancy way of saying that the virtual function of a derived class can now return an instance of the derived class when the base class virtual function it is overriding returns an instance of the base class. It's an important design idiom in the support for class hierarchies, and it is a welcome addition to Visual Studio .NET.
Here is an example of how it is typically used. Consider this abstract Query base class:
In the NotQuery derived class instance, clone returns a copy of a NotQuery object. For example:
Without support for covariant return types, the return type of the NotQuery instance of clone must be Query*:
This works fine if you are assigning the return value of clone to a Query* pointer, like so:
But this fails when you want to explicitly assign its value to a NotQuery*, as in:
With covariant return type support, you can explicitly declare the NotQuery clone instance to return a NotQuery*:
This allows you to use clone in an intuitive fashion without requiring an explicit cast:
While explicit initialization of a static const integer member is not as critical a language feature as that of covariant return type, it provides an important design convenience in class situations requiring a constant expression, such as the definition of a fixed-size array. For example:
A static const integer member is the one instance in C++ in which a class member can be initialized explicitly within the class definition, thereby making the value available to other constructs within the class, such as the dimension size of m_buffer. Without this construct, the use of an enumerator as a symbolic constant was a common workaround.
The return value of main represents a program's exit status. By convention, an exit status of zero indicates program success. With standard C++, a main function without an explicit return value means the compiler inserts a
prior to the end of main. With Visual Studio .NET, Visual C++® is finally compliant with this standard.
Managed C++ in Visual Studio .NETThe Managed Extensions for C++ in Visual Studio .NET allow for three primary application strategies:
Query is the abstract base class of an object-oriented hierarchy for interpreting a query language. Figure 3 shows how a query session might run. The native invocation of the text query system looks like this:
I want to expose the TextQuery interface to the .NET platform without having to touch the darn thing, let alone consider reimplementing it. After all, it works. I wasn't sure if I wanted to move to the .NET platform if it meant I had to give up on my investment in native code. Fortunately, wrapping native code to expose it to the .NET platform is an important benefit of managed C++. Figure 4 shows what the code for doing this looks like.
The __gc modifier (shown in Figure 4) identifies the class as a managed class—a garbage collected class allocated on the common language runtime's managed heap. I declare the native class as a pointer member. I allocate it on the unmanaged heap through the new expression, the same as I do in native code. Because it is not garbage collected, I delete it within the destructor. Both build_up_text and query_text serve as stub functions, dispatching the call to the actual TextQuery object.
Figure 5 shows the revised main function, generated by the managed C++ project wizard.
Now let's discuss how to make use of the .NET Framework. If someone were to ask me what is the most significant weakness of ISO Standard C++, I would say it's the fact that there is no standard library for programming domains such as threading, network programming, regular expressions, XML, and so on. The Standards committee is planning to address this deficiency in its next cycle, but that's years from now. What do you do in the meantime? Fortunately, the .NET Framework provides an attractive solution. For example, Figure 6 shows a simple Socket server class utilizing the .NET Framework. It accepts phone number queries for employees of Northwind, the sample SQL database distributed with Visual Studio .NET.
Figure 7 is a sample output created by running the server and recording its handling of three clients. Figure 8 shows the implementation of the Start function shown in Figure 6. The Start function makes use of the Thread class to spawn independent threads to retrieve the data from the database and handle client connections. Because WriteLine requires a reference parameter, you need to explicitly box the port value.
The Exception class is the root of the .NET exception hierarchy. The ToString method displays a full stack trace, which is very cool. ThreadStart is a delegate type—a kind of generic pointer to both static and nonstatic member functions.
I could go further into the implementation, but I think you can get a sense of the framework's power and ease of use. More importantly, you can see how easy it is to make use of it through C++.
The Future of C++Hopefully after reading this brief look at C++ in Visual Studio .NET, you feel reassured that Visual C++ is not only still a member of the family, but an important family member at that! To underscore that importance, the Visual C++ team at Microsoft is working hard on an interim release to deliver these features as soon as possible. In terms of ISO Standard C++, the team has made exceptional strides in compliance work. For the power programmer, this means templates, templates, templates. Third-party libraries that heavily utilize templates, such as Loki and Boost, now compile internally without workarounds. As they say in Hollywood, stick around. You ain't seen nothing yet!
| For related articles see:|
Visual C++ .NET: What's New for MFC Developers?
Visual Studio .NET: Managed Extensions Bring .NET CLR Support to C++
Bugslayer: Optimize and Trim Your Code with New Switches in Visual C++ .NET
Hello World in Managed Extensions for C++ For background information see:
Essential C++ (Addison-Wesley)
| Stanley B. Lippman is a new architect with the Visual C++ development team at Microsoft. He spent more than 12 years at Bell Laboratories where he worked on the original cfront C++ implementation. Stan is the author of C++ Primer, Inside the C++ Object Model, Essential C++, and the recent C# Primer, all published by Addison-Wesley.|