Why C++ is still my favourite language (Revised)

With the advent of .NET the Microsoft camp is also moving away from C++ to a virtual machine driven architecture a la Java. When Java first came out the advantages were supposed to be memory management and security. Garbage collection removed the mundane memory management responsibility away from the application developer to the virtual machine – or so the claim goes. Unfortunately people doing Java/.NET forgot to close files and database connections and had to go back to basics. .NET provided the ‘using’ feature that reintroduced deterministic destruction in a rather clumsy way. Besides the try-finally syntax was supposed to take care of relinquishing resources (other than memory) that were not needed.

In C++ we use RAII. If an object encapsulates an database connection class then a database connection is opened in the constructor and closed in the destructor. No messy try-finally clause. Any good C++ programmer knows to take care of memory allocation using RAII. So memory management is less of an issue these days. The second advantage of C++ is the template programming. Generic programming in .NET is a pale shadow of template programming in C++. Here is an example:

template <typename T>
class TContainer
T myObj;

TContainer(T const& obj): myObj(obj){}
~TContainer() { myObj.CloseHandle(); }


Notice the ‘CloseHandle’ call in the destructor. This template demands that the class that T is instantiated with implement ‘CloseHandle’. In C# a workaround can be provided using ‘where’ except you have to define a separate interface that lists all the methods you plan to use, which is a bit cumbersome. C++ template programming is not for the faint hearted. Still you don’t have to go there unless you wish to write libraries for use by other people (third party if you may).

The one advantage of C# is reflection. In C++ we could query the typeid but we cannot traverse the entire set of methods and interfaces an object provides. This feature is used heavily in a number of instances like data binding as used by a GUI programmer.

Nevertheless, if you are going to develop algorithms, realtime, or even multimedia C++ is the way to go.


About The Sunday Programmer

Joe is an experienced C++/C# developer on Windows. Currently looking out for an opening in C/C++ on Windows or Linux.
This entry was posted in Software Engineering and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s