Hey guys i have a quick question. Im currently using a const char to print out a message that is printed to the screen alot eg.
should i use the string class instead or is it a matter of preferance?Code:const char welcomeMsg[6]"Hello";
Printable View
Hey guys i have a quick question. Im currently using a const char to print out a message that is printed to the screen alot eg.
should i use the string class instead or is it a matter of preferance?Code:const char welcomeMsg[6]"Hello";
Because the message is constant, ie, it's not supposed to change, an STL string would be a bit of overkill. When you need to parse, split, or grow a string, use STL.
Ok cool thanks Benny :)
>Because the message is constant, ie, it's not supposed to change, an STL string would be a bit of overkill.
Even if it's not supposed to change, the member functions and overloaded operators of the string class can be mighty useful. Consider the following method:
Now, the advantage of strings is diminished because to compare with welcomeMsg (assuming the non-const strings are declared std::string X;) you still must include <cstring> and use strcpy and friends:Code:const char welcomeMsg[] = "Hello";
The same goes for every other operation with welcomeMsg that requires a C-style string function. On the other hand, if welcomeMsg were declared as such:Code:if (strcpy(X.c_str(), welcomeMsg) == 0) {
// Equal
}
The complex comparison for equality would simply be:Code:const std::string welcomeMsg("Hello");
And the same advantage is true for any other overloaded operators. Another advantage is that now you can work easily with welcomeMsg if the need arises without resorting to C-style string functions or hand-rolled functions if the C library doesn't support what you want in some fashion. The C++ string class has a respectable number of member functions that are useful even with const strings.Code:if (X == welcomeMsg) {
// Equal
}
Conclusion? Don't mix C and C++ strings when you can avoid it. This almost always results in not using C-style strings and a considerable improvement in the quality and robustness of the code. :)
Don't you mean strcmp()?Code:if (strcpy(X.c_str(), welcomeMsg) == 0) {
// Equal
}
Indeed, that code compares a char pointer with 0, which has to be considered an error.
>Don't you mean strcmp()?
Yes. My fingers were running on autopilot. :D
I still think it's overkill for a simple welcome message.
ya i have no need to compare, add to or anything like that im just using it so i don't have to type so much :)
>I still think it's overkill for a simple welcome message.
The C++ string class was designed to be comparable in size and speed with C-style strings. Also, because of the hidden implementation, library writers have quite a bit of freedom in making std::string superior to C-style strings in the way of performance and space costs. I think your hesitation is unfounded. :)
I think there are some advantages to localization if he stored the data in a central location such as a resource(etc.) There, I think he'd pretty much have to store c-style strings. Besides, even when writing code such as std::string("Hello") the "Hello" characters will be stored as static c-style array.