Some important C++ syntax

Some important C++ syntax

Postby Cuchulainn » Fri Feb 16, 2007 2:35 pm

I would like to introduce a number of advanced C++ syntax constructs that promote developer productivity and the reliability of code:



1 Template class and template functions: C++ supports the generic programming metaphor. This is the realisation of the Abstract Data Type (ADT) in computer science. An ADT is essentially a data structure and operations acting on that structure. But the type of the data is unspecified or generic. C++ supports a wide range of ADTs in its Standard Template Library (STL). Examples are lists, vectors, maps and sets.



The advantages of templates are:



. Once you have written and tested a template class it can be reused in many contexts by replacing the generic type by a specific type (this is called template instantiation)

. They are compile-time, hence fast

. They complement the object-oriented programming paradigm. You can inherit a template class from another template class, for example

. In some cases, using the ubiquitous inheritance mechanism is just not the correct solution. In particular, deriving all classes from the ?cosmological? object or Object leads to performance and reliability issues. Using templates in this way avoids dynamic casting



2 Exception handling: C++ supports try/catch/throw and the use of this technique is to be advised rather than ERRNO variable or even assert(). In general, exception handling is useful for catching and handling logic errors in your code. For example, here is a piece of code for printing arrays in Excel. The called functions checks if the input arrays from a finite difference methods are aligned:



try

{ // Print option price

printMatrixInExcel(fdm.result(), fdm.TValues(), fdm.XValues(),

string("BSEulerE"));

}

catch (DatasimException& e)

{ // If arrays are not compatible catch the exception here



e.print();

return 0;

}



In this case the function must be called with the arguments in the correct order, otherwise a run-time error will result because of array misalignment. Having an exception handler allows to pinpoint the logic error.



3 STL has a number of generic algorithms for sorting, searching and modifying STL containers. Please use these rather than creating your own.



4 The last technique is more of a tip. The misuse of the inheritance mechanism is harmful in my opinion. Many C++ applications become difficult to maintain for the following reasons:



. Using deep C++ class hierarchies (deriving a class from a class from class ?)

. Multiple inheritance considered very harmful

. Creating classes with many member functions



These problems can be avoided by first designing the application and using the appropriate design patterns.



Some C++ applications start small and in the early phases functionality, performance and accuracy are important factors for success. When the application is accepted it needs to be extended, at which time maintenance and ease of extension become important, especially when time-to-market forces come into play.
User avatar
Cuchulainn
 
Posts: 669
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Return to C++

Who is online

Users browsing this forum: No registered users and 1 guest

cron