char* VS string

Started by
16 comments, last by Seriema 22 years, 1 month ago
When working with null-terminated strings (text, in other words), either the STL 'string' or your own string class are good choices. But character arrays have their place. For one thing, it's better to pass them as function parameters than string objects, because there is less overhead. The function can then convert the array to a local string object.

~CGameProgrammer( );

Edited by - CGameProgrammer on February 23, 2002 11:32:57 PM
~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
Advertisement
quote:Original post by bctorvik
As the Ap said, you can use c_str to convert stl strings to char*, but I''ve found that sometimes a function cannot take in the const char* that is returned from c_str.


This is because your functions are not const-correct. Fix that... read any good C++ book on where and when to declare things const, and your problems will clear right up.

quote:Now maybe there''s another, more visually pleasing, way to get around that, but I''ve found doing &stl_string.at(0) works like a char* without the const nuisance.


This is BAD BAD BAD BAD BAD. There is a REASON c_str() returns a const value: it doesn''t want anything else changing it. If you do so (and since you''re throwing away the const, you might even not realize it... this is why const is in the language) really strange and hard-to-debug things will start happening, since the string takes up a different amount of space than the string object thinks it does.

Besides, one of the main reasons to use the STL is to make your code readable. your &at(0) idiom is not a construct most STL programmers would immediately understand, so if you can avoid it, you should.

If you REALLY need to use a const variable in a non-const context (and, in this situation, you definitely don''t), use const_cast.
By all means, I wasn''t trying to state the way I presented as the IDEAL way of doing things, but it does work in as much as I''ve seen. An example of why I''ve used this is the following: you''re given a big library of ugly code that DOESN''T use good programming practices (i.e., using const, as you stated). You could go through the 10+k lines of code and change all the functions that are not declared const char*, or you could get around it the way I proposed. As long as lives don''t depend on your code, what I said should do you just fine

Of course you should program using good practices when you''re creating a program from the ground up; it was just an example
"Me lose brain cells?" *laughter, then a pause* "Why I laugh?"
Hey, if you''re picky, why not
#define string char*
And don''t waste time on integer to string, that what
itoa(,,) is for.
We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)
std::string implementations also usually use reference counting, so copying long strings is instantaneous, and the string knows when its modified to create a real copy of its data.

char* never goes away because it''s useful for byte streams (although I use unsigned char* myself for byte streams).
quote:Original post by krez
perhaps you should face your fears and just learn how to use char*... before it is too late...


Too late for what exactly?

--

When horses lift their heads up high to look at something, they're looking far into the distance. To see things that are closer, they lower their heads.

Edited by - SabreMan on February 25, 2002 6:29:09 AM
quote:Original post by SabreMan
Too late for what exactly?

why, too late to save yourself from the Dark Side of the Force, of course!
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
I prefer a char array just for easy algorithm stuff. But, I seem to lean torwards string myself.
---START GEEK CODE BLOCK---GCS/M/S dpu s:+ a---- C++ UL(+) P(++) L+(+) E--- W++ N+ o K w(--) !O !M !V PS- PE+Y+ PGP+ t 5 X-- R tv+ b+ DI+ D G e* h! r-- !x ---END GEEK CODE BLOCK---

This topic is closed to new replies.

Advertisement