RE: [Dev-C++] [OT] C++ string vs char *
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ioannis V. <no...@ya...> - 2000-12-30 12:13:51
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...]On Behalf Of
> Ramana Kumar
> Sent: Saturday, December 30, 2000 10:56 AM
> To: dev...@li...
> Subject: RE: [Dev-C++] [OT] C++ string vs char *
>
>
> I'm not too good with strings, but based on your e-mail, I would go for
> creating a simple string class.
>
> C++ strings become a pain depending on the things you do because
> they aren't
> compatible with char[]s and vice versa (I'm writing a game that takes in a
> string containing a command in English and acts accordingly, i.e. "Open
> door" opens the door).
I did not understand what exactly you mean by saying they are not compatible
with each other, but you can work with both with C-style strings and C++
strings either by convering the C-style to C++ style or by using the C++
style string functions so as to use them as C-style (although i recommend
the first conversion from C-style to C++ style).
e.g. the string function c_str() enables you to use C++ strings with
functions accepting C-style sttrings (example taken by Bjarne Stroustrup's
"The C++ Programming Language" - Special Edition page 49):
[...]
string name="Niels Stroustrup";
void f()
{
printf("name: %s\n", name.c_str());
}
Ioannis
|