Coding Horrors: C++ and Overloaded Methods

My tough feelings for C++ as a language are all but new.  Here is another example why this language is not suited for children, and young students should not take C++ as their first programming language, unless they are really smart and have solid common sense to protect them from the bad influence of this language.

Consider the following code:

Nothing special, right?  It looks absolutely logical that the call to myFunction in main() would invoke void myFunction(String, String, bool = false).  Well, it won’t.  The function that will get called is void myFunction(String, bool = false).

Well, I must admit that I myself would not figure out easily.  Further reading revealed the explanation - the compiler is using the more cost-efficient conversion of char * to bool, rather than creating a new instance of class String.  Further reading proved that the behaviour of the compiler is strictly according to the C++ Standards!

Feel free to call me any names you can think of, but for me the authors of C++ Standards live in a parallel, sick universe.  Guys, have you heard that code has to be readable, too?

P.S. Edit: fixing typos.

This entry was posted on Thursday, April 9th, 2009 at 2:12 pm and is filed under Just Thoughts. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

 

Leave a Reply