Originally Posted by
sean_mackrory
1) I'm more used to C-style strings
Can't argue with that. Oh, perhaps you should get more used to C++ strings.
Which is really not an issue in a program that uses cout and cin.
3) Personally I thought C-style strings would be easier for newbies to grasp since they wouldn't have done much OOP yet, but I'm not one to argue with someone with as much knowledge of the language as Prelude.
It's not about knowledge of the language, it's about common sense. The C++ string behaves like a built-in type, it just makes sense. The C-style string doesn't.
Just a few examples. I'm assuming "using std::string". I want to put a string literal into a variable.
Code:
C++:
string str = "Hello, World!";
C:
const char *str = "Hello, World!"; /* What is "const char *"?
Why can't I say "char *"? The compiler allows it. Why can't I modify it? */
char str[] = "Hello, World!"; /* "char []"? Weird syntax. */
Yes, the syntax becomes second nature to C programmers, but newbies?
String copying with unknown size:
Code:
C++:
string str2 = str1;
C:
char *str2 = malloc(strlen(str1)+1);
strcpy(str2, str1);
/* And don't forget to free! */
Again, all those questions. What is malloc? What is strlen? Why can't I just say str2 = str1? Why do I have to remember to free? Why shouldn't I return such stuff from a function?
Concatenation:
Code:
C++:
str1 += str2;
C: (assuming that str1 was allocated with malloc)
char *t = malloc(strlen(str1)+strlen(str2)+1);
strcpy(t, str1);
strcat(t, str2);
free(str1);
str1 = t;
The code is getting longer.
There are more issues. Buffer sizes. Why do I have to watch for buffer overflows? What is that, anyway? Why can't I return a local buffer from a function?
And have you so far seen any signs of OOP in the C++ version? Anything that newbies would have to understand in order to use string? No, since it's a perfect blackbox. It might as well be built-in.
Bottom line: newbies usually want to do things now, and later learn the whys and hows. It's simply more fun that way.