|
-
Mar 7th, 2003, 10:51 AM
#1
Thread Starter
Lively Member
char*
using char* to store a string isn't c++ standard, right? i am supposed to use the string class, right? thanks for the tip
-
Mar 7th, 2003, 11:04 AM
#2
Frenzied Member
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++
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Mar 14th, 2003, 01:02 PM
#3
Thread Starter
Lively Member
PHP Code:
const char g_szClassName[] = "myWindowClass";
(this is from the 2nd winprog tutorial..i know its intended to be c )
having this in a c++ program wouldn't be good, right? but how would i replace it?
PHP Code:
string g_sClassName ...
?
thanks guys
-
Mar 14th, 2003, 01:20 PM
#4
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:
Code:
const char * const g_szClassName = "myWindowClass";
This creates an absolutly non-modifyable string.
And better yet, because windows applications should be written in a generic-character way:
Code:
const TCHAR * const g_szClassName = TEXT("myWindowClass");
If you have any questions about that feel free to ask.
std::string is supposed to be used for strings that you work with, that get modified, searched etc.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 14th, 2003, 08:02 PM
#5
Thread Starter
Lively Member
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)
-
Mar 14th, 2003, 08:11 PM
#6
Thread Starter
Lively Member
ohh by the way, if i have
PHP Code:
const char* const classname = "blahblah";
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:
char* str = "string";
// is not the same as
char str[] = "string";
i know the second one is an array of characters..the first one is a pointer to an array of chars?
-
Mar 15th, 2003, 12:45 AM
#7
Thread Starter
Lively Member
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
-
Mar 15th, 2003, 08:52 AM
#8
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 do
Code:
const TCHAR *szString = "Hello, World!";
then it compiles fine as ANSI, but as UNICODE you'll get an error like
"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:
Code:
const wchar_t *szWideString = L"This is wide.";
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.
If you include <tchar.h> (only MSVC++) you can also use the shorter _T macro instead of TEXT.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 15th, 2003, 01:42 PM
#9
Thread Starter
Lively Member
thanks a lot CornedBee for your extensive reply! its much clearer now
-
Mar 19th, 2003, 05:22 AM
#10
Lively Member
CB,
const char *str = "string";
Would not make the value const rather than the pointer?
Should it not be:
char* const str = "string";
-
Mar 19th, 2003, 07:20 AM
#11
Monday Morning Lunatic
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.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Mar 19th, 2003, 03:13 PM
#12
const char *
makes the value const
char * const
makes the pointer const.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 20th, 2003, 04:12 AM
#13
Lively Member
Originally posted by CornedBee
Because the location in your exe is read-only the pointer should be const:
const char *str = "string";
CB,
Thanks, I was confused by what you said above.
-
Mar 27th, 2003, 12:58 PM
#14
Lively Member
or you can write your own stringclass and it ain't that difficult
-
Mar 27th, 2003, 01:43 PM
#15
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|