Qt is a really wonderful and nice toolbox, and I thoroughly enjoy working with it, so it’s unfortunate that I’m using it for my example of one of the many things wrong with C++. In my opinion C++ is a kludge for bolting the Object Oriented paradigm onto a language barely more sophisticated than assembly. This unholy marriage has resulted in a language that is rife with special-case exceptions and horrifically complicated syntax. Just take a look at the STL and you can actually feel the hemorrhage of pre-mature optimization.
Anyway, Pointers are long known to be the most problematic concept for students of C/C++. Truly grokking pointers took me about 2 weeks initially, and I still get to enjoy learning another nuance about every other month. The ability to control the memory of your machine is a power not to be accepted lightly. We had an opportunity to leave pointers behind with Fortran and Lisp, but K&R apparently enjoyed living at the machine level. To this day we still get to hold pointers responsible for much of program ailments. (buffer overflow, memory leaks, null dereference, etc…)
One of the other real sticky issues that novices have to deal with is the difference between the stack and the heap. This is usually introduced alongside the introduction of pointers, because the two concepts are rather intricately related. But all this just makes the conceptual cliff a bit steeper. Eventually, the junior programmer develops a nice intuition about allocation and optimization, and when to use stack vs when to use head and handle the memory yourself. Then, the awesome power granted to us via pointers rears its ugly head.
Straight from the Qt Documentation on QString
Note for C Programmers
Due to C++’s type system and the fact that QString is implicitly shared, QStrings may be treated like ints or other basic types. For example:
QString Widget::boolToString(bool b) { QString result; if (b) result = "True"; else result = "False"; return result; }The result variable, is a normal variable allocated on the stack. When return is called, and because we’re returning by value, the copy constructor is called and a copy of the string is returned. No actual copying takes place thanks to the implicit sharing.
Leave a Reply
You must be logged in to post a comment.