Quote:Original post by MikeTacular
The rule of thumb is to use std::string whenever possible. It's part of "good" C++. Checking user input (or any other form of text) is going to be a lot easier if it's a std::string rather than a char array. Example:std::string name = getSomeName();if (name == "Bob"){ doSomething();}char* name = getSomeName();if (strcmp(name, "Bob") == 0){ doSomething();}
You tell me which of the two is cleaner (if you say the second one you're crazy [smile]).
hehe. I remember bits and pieces from my studies for my computer science degree. One of them was about ambiguity when overloading the "==" operator for comparision operations, because its C++ it could mean anything you like. Using the function strcmp described a little more about what the function did. For example in the first example its unclear (if you had never seen the string library before) whether the comparison is case sensitive or otherwise (I guess you could argue the same case for the second example, but at least the comparison is a little more descriptive as too what it is comparing).
also, when I was learning Java and was confronted with string classes, I got very frustrated with the higher level of abstraction it provided; I felt it was a little too much away from "just storing some numbers in an array". Of course they're big advantages to the string class, but sometimes a char array will do. I hate having to trawl through documentation detailing how to get an ascii character number out of a string when I've forgotten the function name, or visa versa when I know its probably stored as an ascii number anyway. (I know, because I'm the one who put it there)
I do truely believe there is nothing wrong with a char array, as there is nothing wrong with using the string class to store strings; its all about abstraction and who provides that abstraction, the api or the programmer. A bad programmer (or one of little understanding of how a string is interpreted by the hardware) is going to misuse a char array. If somebody looks over my code and frowns at my char array, and the only answer they can give as too why its bad is to say "you should be using the std:string class instead"; I'd like to belt them into submission until I get a more rational explanation out of them. One method should be used against the other when you're sure you know the differences and why you have choosen the method.
rant over.