• Embed Doc
  • Readcast
  • Collections
  • CommentGo Back
Download
 
 Migrating From C++ To C#
Introduction
Since its beginning in the 1980s, C++ has come a long way. It has a largeestablished user base, tested software, its own tools (compilers, etc), and lots ofexperienced programmers. It has also developed its own idioms and techniquesfor programmers to write effective software. C++ programmers are comfortablein getting things done with the facilities that are provided with it in an efficientmanner..NET is a powerful new platform with a great deal of promise. C# is designedfrom the ground up to harness the power of this new framework. It provides awhole host of features and is strongly based on C++. C# is an object orientedlanguage and is the first component-oriented language in the C family. It alsomakes writing Windows and Web applications faster and easier. C# is gainingwide acceptance and it is clear that it is here to stay for a long time.C# is not a replacement for C++, and it is more than likely that both will be usedwidely for the foreseeable future. However, there are many practical cases wherethere is a necessity to migrate from C++ to C#. For instance, your company'spolicy may be to change all existing code to .NET, or perhaps you wish to takeadvantage of some of the facilities made available in .NET.The question is, how do we make the transition as smooth as possible whilegetting the best results? Adopting a new language doesn't just mean convertingthe existing code from C++ to C#. By just knowing the syntax, a C++programmer cannot straightaway start programming in C#. These twolanguages differ largely by their design and approach towards problem solving,which makes the language transition harder.
System Requirements
It is preferable that the reader has access to the C# compiler available inMicrosoft Visual C#.NET. This case study is for those programmers coming froma C++ background, who are new to C# or have just started programming in it.The programmers with a good understanding of C# are in a better position tounderstand the approaches taken in the conversion process.
Case Study Structure
The case study consists of three main sections:
 
The approachIn this section, we briefly cover the basic theory that is necessary forunderstanding the issues in conversion. It is possible that you may not be clearabout few of the C# features mentioned here - they are covered in the followingsection.
Comparing C++ and C# features:In this section we look at the different features of the two languages which arenecessary to make the conversion possible.
Steps in converting existing code:The steps that are required for converting the existing code from C++ to C# arecovered in this section. An example of converting class hierarchies from C++ toC# is also covered.
The Approach
What is the best approach for getting equipped for a smooth transition from C++to C#? Understanding! Migrating from one language to another involves aconsiderable effort. This is not because of a change in syntax, rather because ofchanges in methodology - design approach, underlying technology, and theapproach towards problem solving. Understanding that there is such afundamental shift, and having the knowledge of where the major differences lie,will help a lot.The underlying translation models for C++ and C# are quite different. C++follows a static linkage model, meaning that the source code is compiled by thecompiler to result in object code. The object files are linked to result in anexecutable file. The operating system loads and controls the execution of theprogram. The language features are designed with this approach in mind. Forexample, there is no support for reflection. Moreover, the code is only sourcecode portable and not much runtime support is available.C# follows an entirely different translation model - it combines compilation andinterpretation. The source code is converted to an intermediate format known asMSIL (Microsoft Intermediate Language). A virtual machine, referred to as theCLR (Common Language Runtime), takes over to execute the instructions. Theexecution is thus in the hands of CLR and the code executed is referred to asmanaged code. This change in translation model is reflected in the languagefeatures as well.
 
A very important difference is in the area of memory management: theprogrammer no longer has the complete control of the lifetime of the objects inthe heap. The garbage collector takes care of deleting objects whose lifetime isover. So there is no need for the keyword "delete". However, there aredestructors in C#. If the "delete" keyword is not available in C#, then what is theuse of destructors? In reality, the destructor syntax in C# is very misleading,especially for programmers from a C++ background. They are actually finalizersthat are called before an object is garbage collected.Another issue to understand is the change in design criteria. C++ is designed forexperienced programmers in mind and 'trusts the programmer' (in the Ctradition). So no extensive runtime checking is done, there are implicit casts andpromotions in function calls. These features have proven to be very useful, butalso very bug-prone. Therefore, only experienced programmers should use them.However, C# is designed so that even novice users can learn it fairly easily, andis also designed with robust software in mind. It performs extensive runtimechecking with very few implicit conversions and tries to make the life of theprogrammer easier.How can this understanding of language design change help in transition fromC++ to C#? Let us use an example. A single argument constructor also serves thepurpose of a conversion operator in C++. When a conversion is required, thatconstructor will be called implicitly because, it 'trusts the programmer': it isassumed that the C++ programmer is aware of it. Such implicit calls may lead tosubtle bugs, like:
class Stack{public:Stack (int initivalCapacity);// constructor that takes int as an argument// other members};// now consider the codeStack s;s = 25;// implicit conversion, a new Stack object is created with int asargument// s = Stack(25);// beware! the programmer may have programmed without being aware thatthe// constructor with int argument is called for the conversion operation// from int to Stack
To avoid such problems, you cannot use single argument constructors asconversion operators in C#. You have to support explicit conversion operatorsfor that. Also, you can appreciate the use of implicit and explicit keywords in C#
of 00

Leave a Comment

You must be to leave a comment.
Submit
Characters: ...
You must be to leave a comment.
Submit
Characters: ...