wrote:
> Ben Pope wrote:
>> std::string is much easier to use correctly than char*.
>
> Yes.
>
>> Surely the basics are std::strings, and the complicated bits are arrays,
>> resource management, and pointers?
>
> Sort of.. (see below)
>
>> Your comments seem backwards somehow.
>>
>> Only when somebody is at ease with the basics, should they progress to
>> the more advanced topics of pointers, arrays and resource management.
>
> The "usage" of std::strings is easier than char* of course. But
> std::string hides implementation details and simple language
> mechanism's (buffer allocation and deletion etc) that the developer
> should be familiar with in order for them to apply such mechanism's to
> their own classes and algorithms.
Yes, but they should only need to apply those things to their own
classes and algorithms *after* hello world, and whatever else comes
after that.
> The concept of arrays may be more advanced, but a character array is
> IMHO the simplest, most concrete way to explain those features and
> visualize them. It's much easier to explain an array of chars than an
> array of X objects.
std::vector is much easier to use than a dynamically allocated array.
> One should be aware of what allocates memory where and who deletes it
> before diving into library functions that do it for you.
Why? Keeping that horrible resource management stuff hidden (in
constructors and destructors - RAII) is what we all strive to do in
order to create code that is not leaky and exception safe, surely?
Why should a beginner learn how to misuse dangerous tools from the very
beginning (when they don't need to), only to be set straight later on?
> This may seem backwards - heck, it may even actually be backwards - but
> I believe it's a better way to learn.
I guess that you think that you should learn C before C++?
> Also, the recommendation of "Heck, just use std::string" usually comes
> at a point where somebody is fundamentally misunderstanding what a
> char* does. So I definitely think it's better to explain that first,
> before just pointing towards a library feature.
I disagree. Get inexperienced people to use the features that have been
painstakingly designed and refined over time and slapped in the standard
library, instead of reinventing the wheel.
The features have to be in standard-conforming implementation. They are
there to hide all the messy details of resource allocation, provide
value-semantics and generally make your life easier.
The use of raw pointers is really only necessary in very low-level code,
and at that point you really do need to understand in detail what every
line of code *really* does... such as whether it can throw an exception,
in order to avoid resource leaks.
Pointers and the free store are often unnecessary in simple programs,
and even then, the use of references rather than pointers mitigates the
chance of many errors.
The moment you introduce pointers you have to introduce not just
reference semantics, but exception safety and resource management
techniques such as RAII. IMO this is too much to grasp in one go,
especially for a beginner.
> Otherwise one could simply say "Don't bother with char*'s, just use
> Visual Basic!"
I'll refer to your previous comment:
> The "usage" of std::strings is easier than char* of course. But
> std::string hides implementation details and simple language
> mechanism's (buffer allocation and deletion etc) that the developer
> should be familiar with in order for them to apply such mechanism's to
> their own classes and algorithms.
So I guess everybody should be taught machine code initially. C++ is
way too abstract!
Abstractions are what we try to model in our software, in order to not
have to think about the details. The more details that can disappear
behind an interface, the better.
Ben Pope
--
I'm not just a number. To many, I'm known as a string...