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
class TContainer
{
T myObj;
public:
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# (and Java too I think) a workaround can be provided for this simple case by casting myObj to a particular interface and then using the interface. But that's a runtime feature, and does not scale.
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 including data binding as used by a GUI programmer. I am not quite sure why C++ does not do it. Even the dependency injection pattern cannot be implemented in C++.
Nevertheless, if you are going to develop algorithms, realtime, or even multimedia C++ is the way to go.
No comments:
Post a Comment