using char* to store a string isn't c++ standard, right? i am supposed to use the string class, right? thanks for the tip
Printable View
using char* to store a string isn't c++ standard, right? i am supposed to use the string class, right? thanks for the tip
You can use char * to store a C string (A null-terminated char array), but yes, using the string from the standard library is recommended in C++
(this is from the 2nd winprog tutorial..i know its intended to be c :) )PHP Code:const char g_szClassName[] = "myWindowClass";
having this in a c++ program wouldn't be good, right? but how would i replace it?
?PHP Code:string g_sClassName ...
thanks guys :)
It's good because it is a constant. Using the string class for a constant is a waste of memory and computation time.
But it is bad too: it still might waste memory. Since it is a constant it should be a pointer:
This creates an absolutly non-modifyable string.Code:const char * const g_szClassName = "myWindowClass";
And better yet, because windows applications should be written in a generic-character way:
If you have any questions about that feel free to ask.Code:const TCHAR * const g_szClassName = TEXT("myWindowClass");
std::string is supposed to be used for strings that you work with, that get modified, searched etc.
what do you mean by generic-character way? i looked in the headers and saw that TCHAR was a typedef of wchar_t.. is that the wide character type i hear ppl talking about?
thanks for clarifying..so basically i am 'allowed' to use char* only for constant stuff..otherwise, i use the string class for manupulation..
got it :) (i'll look into that TCHAR type)
ohh by the way, if i have
how would you define classname? a constant pointer to an array of character? i am asking because i am always confused about this:PHP Code:const char* const classname = "blahblah";
i know the second one is an array of characters..the first one is a pointer to an array of chars?PHP Code:char* str = "string";
// is not the same as
char str[] = "string";
cornedbee, when would i use the TEXT () macro, and sorry, but i couldn't find any precise info as to what wide character is in the msdn..could you give me some info? thanks a lot for your time :)
Ok, one by one.
First:
const char* const classname = "blahblah";
classname is a constant pointer to a constant character string stored directly in your exe file.
Second:
char* str = "string";
char str[] = "string";
The first is a pointer to a location in your exe file where the string "string" is stored. Because the location in your exe is read-only the pointer should be const:
const char *str = "string";
The second is a character array that gets initialized with the string "string", this means it is a 7-element array with the character values 's', 't', 'r', 'i', 'n', 'g' and '\0'.
About wchar_t: wchar_t (or WCHAR or whatever) is a data type that stores wide characters, characters from the UNICODE UTF-16 standard. Windows NT, 2k and XP internally work only with wide characters. If you pass an ANSI (char) string to the API it gets converted to UNICODE. Any results are converted back.
Windows 9x and ME don't know UNICODE properly unless you install the UNICODE layer, which does the opposite of the NT ANSI layer.
WinCE finally knows only UNICODE and can't work with ANSI.
To sum up: NT works with both character types, but is faster with UNICODE. 9x only works with ANSI unless you install additional DLLs. CE works only with UNICODE.
Now this is a mess. What character type to use? The solution is the generic character type TCHAR. Depending on the macro UNICODE being defined this type equals either wchar_t or char. By always using TCHAR you can compile for any character type simply by defining or undefining UNICODE.
There's a problem however. In C source something like this:
will always be seen as ANSI string by the compiler. Now when you doCode:"Some String"
then it compiles fine as ANSI, but as UNICODE you'll get an error likeCode:const TCHAR *szString = "Hello, World!";
"typecast: Cannot implicitly convert from const char * to const wchar_t *. Conversion requires a reinterpret_cast or C-style cast."
To get a UNICODE string literal you have to prepend an uppercase L:
But this is not generic. That's what the TEXT macro is for. Enclose all literal strings in the TEXT macro and they'll be UNICODE when you need UNICODE and ANSI when you need ANSI.Code:const wchar_t *szWideString = L"This is wide.";
If you include <tchar.h> (only MSVC++) you can also use the shorter _T macro instead of TEXT.
thanks a lot CornedBee for your extensive reply! its much clearer now :)
CB,
Would not make the value const rather than the pointer?Quote:
const char *str = "string";
Should it not be:
char* const str = "string";
That's the point. The string is what's supposed to be const (it's stored in read-only memory). The pointer itself is stored as any other stack variable, and you're free to reassign it.
const char *
makes the value const
char * const
makes the pointer const.
CB,Quote:
Originally posted by CornedBee
Because the location in your exe is read-only the pointer should be const:
const char *str = "string";
Thanks, I was confused by what you said above.
or you can write your own stringclass and it ain't that difficult
That's useless. For constants even the simplest class poses unnecessary overhead, and for non-const strings std::string is almost guaranteed to be better than a string class you write.