The tailor
March 1, 2009
Ok, let’s imagine you’re fed up with the GObject boilerplate code, and decide to use a mature serious language which gives easier access to classes : C++.
C++ followers will tell you how wonderful that language is, where performance with respect to both speed and space is taken very seriously. Read any document about C++ : this language has all bells and whistles, with options everywhere (do I dare saying “A real KDE!”? No, that would be trollish). Let me review some features of this both high and low level language. Performance!
Let’s begin with the most frustating issue, that of inheritance. When you override a method, by default you have done almost nothing : if your object is accessed as being from the base class, then the old method will be called. You have to use the “virtual” keyword on a method you think you’ll need to override later on. If someone else want to override and can’t modify your class, (s)he’s a two-sided toast. On the other hand, the performance!
Sigh, so you add the “virtual” keyword everywhere you think, run your program, and things just go wrong : it’s perfectly normal, you didn’t think C++. Yes, you should declare your base destructor virtual, otherwise when you try to destroy an object through its base pointer, things break, without warning. But with performance!
One more thing about inheritance, and more specifically multiple inheritance : you have to be extra careful in the diamond configuration, because you may end up with copies of data members, which may not be what you had in mind. In such a case, you will again have to use the “virtual” keyword — not where you form the diamond, but on the two branches : again, in C++ you don’t deal with inheritance when it happens, but much sooner. For performance!
The language has also nice things like constructors which get called automatically. Say you wrote an array-like class, with a constructor from integers, and you work with two arrays of integer ; at some point you want to do a comparison tab1[i] == tab2[i], but your fingers type tab1==tab2[i] — ah, you’re comparing a full array with an integer : no problem, the compiler will use the right-hand side to build an array and do a stupid comparison… isn’t it wonderful? Nonsense, but with performance! (for completeness, you saw the previous paragraphs, so I hope you guessed: there’s a keyword to avoid that problem : “explicit”.)
Another case of automatic (and unsafe) promotion is with conversion operators, but I have seen quite a few advices of not using them. I think the “explicit” keyword doesn’t apply to them (yet…).
As closing words, let me explain the link between the title and this post : Fernand Raynaud was a french humorist, and one of its most famous sketches, “Le tailleur” (the tailor) was the story of a man coming back to his tailor to complain about a bad suit. Of course, the tailor finds the suit perfect and puts the blame on his customer, who ends up admitting the suit is indeed perfect, once he has put his left shoulder a little higher, his right shoulder a little back, turned his hips a little, and raised one arm slightly…
PS: if some C++ lover wants to tell me I just don’t have the right mindset, lack the skills or whatever explanation that puts the blame on me and not the language itself : put your comment in /dev/null and read the last paragraph again, tailor!