On Sat, 15 Sep 2012 01:16:01 +0000, LukeWolf wrote:
> As someone who develops in C++ with Qt, I highly recommend this book
> ‘Introduction to Design Patterns in C++ with Qt4’
> (http://cartan.cas.suffolk.edu/oopdocbook/opensource/) and ‘Videos | Qt
> Developer Network’ (http://qt-project.org/videos) . C++ honestly isn’t
> that hard by the way. When people complain about it being hard they’re
> usually complaining about the fact that it grants you the ability to
> manage memory yourself, which Qt makes essentially painless, and there
> are Garbage Collectors for C++ if one must have one. One thing that is
> important to know as well because a lot of people screw up explaining
> this, is that a pointer is merely a pronoun, they try to get too
> technical and miss what it actually is in terms of a lanugage. Also
> with Qt and even STL Strings the perfectly fine to deal with and not an
> “array of characters”.
That’s all fine if you write to QT, but not all C++ coders write to QT.
The beauty of C and C++'s extensibility is also something of a curse for
new programmers, because if you start learning using extended non-
standard libraries, when confronted with a system that doesn’t have those
libraries, you get stuck.
That’s the difference between learning a language’s syntax and learning
how to program, IMHO.
It’s a semantic difference between “it grants you the ability to manage
memory yourself” and “it requires you to manage memory yourself” - a
matter of spin, or “marketing”.
I like the analogy about a pointer, though (although a street address is
usually easier to understand) - the thing in C in particular (but that
carries over to C++) that most find confusing is the difference between
addressing modes and the syntax for declaring a pointer vs. referencing
the value at a pointer.
That’s what tripped me up when I started learning C in the early 80’s.
It took me learning assembly language to really understand handling
pointers properly; until I did that, I operated largely on trial and
error in getting the syntax right.
But the declaration:
char *string1;
(which requires that you malloc the memory unless you want to ultimately
segfault)
vs.
char string2[10];
Means that if you want to reference the address of the array string1, you
do so with the variable “string1”. But don’t forget to null terminate or
do bounds checking when adding to the string (or to increase the
allocated memory), or you’ll run into all sorts of weird issues and
crashes.
If you want to reference the address of the array string2, you do so with
“&string2”.
Things get more complicated with more involved data structures - for
example, arrays of strings (strings being arrays of chars themselves).
Managing memory for those can be tricky.
C and C++ both are great languages to shoot yourself in the foot.
Firsthand experience with that - and in some instances, intentional
firsthand experience (ie, I was writing code specifically designed to try
to crash a system - testing memory protection systems, for instance).
Forgetting to null-terminate the char array can lead to problems with
some functions as well.
Yes, C and C++ can be extended with string handling libraries. Those
libraries may or may not be portable. You can also typedef a “string”
type, but that doesn’t mean that you’ve created a string primitive so
much as created an alias between a user-defined type called “string” and
a char array.
But talking about whether C or a language like Pascal is better/easier to
handle strings with, I can say that my experience has to do with ACM
programming contests - the winningest teams (at least in the early 90s
when the choices were C or Pascal) were not CS teams programming in C,
but engineering teams programming in Pascal. I had the privilege to
train alongside coders on one of the top teams in the world at the time.
That team’s coach explained very specifically that Pascal was the only
language his teams used because of its superior string handling (as most
ACM contest problems involved heavy amounts of string handling and could
not require libraries beyond the standard libraries included with the
compiler provided for competition).
But ultimately, when learning about programming, I stand by picking an
easier language than C or C++ as a starting point in order to learn
structured programming. The most successful programmers I know didn’t
learn a language’s syntax first (other than to be able to build a working
program), but the language facilitated learning about structured
programming techniques (and in later years, OOP principles as well).
If you start by learning an overly complex language’s syntax, you get
stuck with a language and a syntax. If you start by learning about
structured programming principles and OOP principles, then the syntax
will naturally come as you apply the understanding of the underlying
“Latin” of structured programming/OOP.
For me, that was a very good approach, and I highly recommend it for a
number of reasons. It’s not always the easiest approach, but it gives
the most flexibility in applying principles to code written in a very
wide variety of situations, rather than locking you into dealing with the
one language you know, regardless of what libraries are available.
Jim
Jim Henderson
openSUSE Forums Administrator
Forum Use Terms & Conditions at http://tinyurl.com/openSUSE-T-C