C++ is the greatest of all programming languages!

I believe that C++ is the best of all programming languages and cannot believe that a better language will be created any time soon.

Google studies indicated that C++ is signficantly faster than any other language that they studied: See their article touting C++

Many people seem to think (mistakenly in my view) that everything should be easy. But learning to use C++ effectively is not -- but then learning to use the powers of God, were He to grant them to you, would not be easy either. With great power, comes great responsibility. C++ is no exception, but it really isn't all that hard to learn to use C++ like an expert. I have some ideas on the subject myself. See Tips and Tricks of the C++ professionals .

Some people seem to think that languages really don't matter. They are very mistaken. C++ has several features that greatly simplify the act of large scale software development. For example:

Some people claim that C++ is not a truly object oriented language. So? Truly object oriented languages, all of which make the Integer class be some sort of high level object, add enormous runtime costs that have no justification. The return on investment is almost zero when compared to C++ -- which has run time type identification as well as class inheritence with dynamic_cast<T> letting you defer type identification till run time.

In the early 1980s, I was sold on the value of object oriented programming -- even before having any real languages to use. I eventually began using object oriented techniques using the plain old "c" compiler. When C++ came around, I saw that I could better approximate true object oriented programming using constructors, inheritence, etc. I soon saw that perfect object orientation did not add much to my program's speed of implementation or its understandability. I saw no real improvement in those areas until about 2002 when the 3rd generation of C++ compilers were generally available on all major target platforms: all the unixes, ARMs, and Windows. It was around that time that you really could use both templates and runtime type identification the way they were meant to be used.

Some people claim that C++ is hard to use because it leaks memory. This belief persists because of a gross misunderstanding of the language: people are thinking about C and are mistakenly assuming that C++ works the same way.

It is almost trivial to prevent memory leaks. Here's how:

Since I started obeying these simple rules, I have seen a very significant reduction in memory leak bugs -- and I know this because I run either purify or valgrind. I have seen no noticeable degradation in performance of my programs.

To summarize: C++ compilers exist everywhere, provide signficant code automation tools, and with a few simple programming techniques most of the pain and suffering of using plain old C receeds into unpleasant memories.


Examples

Example 1: allocation equals initialization

In many real-world scenarios, variables are changed temporarily while something is done and the variable's original values are restored at the end. Here's a buggy example of trying to do this:

		  // buggy code

		  extern bool locked;  // should be false most of the time

		  bool f()
		  {
		      locked = true;

		      if( function() == 0 )
			return false;

		      locked = false;

		      return true;

		  }

	      
Here, the code is written wrong -- if the function() returns 0, we should also set the locked variable to false. How did this mistake happen? In real world examples, this occurs when some future maintainer adds the if-statement to cause the early return from f(). This person doesn't know to check for all expected end actions and causes a very difficult to debug problem.

To solve this problem, use this code:


		  // Safe code

                  extern bool locked;  // should be false most of the time

                  bool f()
                  {
		      Guard lockVariable(locked, true);  

			// automatically saves and
			// restores the contents of
			// locked.

                      if( function() == 0 )
                        return false;

                      return true;

                  }

              
This works because class Guard's constructor saves the content of 'locked' and its destructor restores it. Here's an example implementation:

		  class Guard
		  {
		  public:  

		      bool *ward_;
		      bool  saved_value_;

		      Guard(bool &ward, bool newValue)
		      : ward_(&ward),
			saved_value_(ward)
		      {
			ward = newValue;
		      }

		      ~Guard()
		      {
			*ward_ = saved_value_;
		      }
		  };


              

Example 2: Writing specialized template algorithms

The modern approach to doing this is to simply rely on the language syntax for template function specifialization. There used to be a variety of really neat work arounds the let you solve the problem in a round-about fashion, but modern compiler's don't require them -- you just do this:

	      template<class IteratorType>
	      void sort(IteratorType first, IteratorType last)
	      {
		// write the solution for the linked list class 
		// and this handles
	      }

	      template<class T>
	      void sort(T *first, T *last)
	      {
		// write the solution for the C++ array case 
		// -- maybe call qsort()
	      }

	      template<class T>
	      void sort(std::vector<T>::iterator first, 
			std::vector<T>::iterator last)
	      {
		// write the solution for the stl vector case 
		// -- you could call qsort
		// or use some algorithm out of a book.
	      }

	      
The nice thing about this code is that the same function signature, sort(first,last), is used for all implementations -- and the compiler picks the right one for you.