|
-
March 4th, 2006, 06:08 PM
#1
Isn't string equals to const char *?
I am reading some documents about c++ and it says:In c++ string is equals to const char *.Is this true?
Because i compile this code:
Code:
#include <iostream>
#include <string>
using namespace std;
int main() {
const char * p="ppppp";
string s="aaaaaaaa";
s+="eeeee";
s=p;
p=s;//gives error:error C2440: '=' : cannot convert from 'std::string' to 'const char *'
}
Isn't string equals to const char *?If equals why does this error occur and why does compiler give permission to change value of string s after initializition?
-
March 4th, 2006, 06:24 PM
#2
Re: Isn't string equals to const char *?
"String" is a term often used to refer to a char* pointing to a zero-terminated sequence of chars, and this is quite safe usage in C. In C++, however, there is a class (in the std namespace) called string, and this is not the same thing, so we usually refer to "C strings" to differentiate the two. The string class does provide automatic conversion to create an object from char* pointers, but the converse (std::string to char*) is not automatic - there's a member function called c_str() to do the job. You can correct the compilation error by putting
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
March 5th, 2006, 02:58 AM
#3
Re: Isn't string equals to const char *?
sawer, you should use C++ strings everywhere, except if you have a very good reason not to do so.
C++ strings are much easier to use and much safer.
Actually, C strings (char*) are not managed automatically, but are plain pointer to chars.
It means that to append two char*, you must allocate memory dynamically (with new[], malloc or perhaps std::vector), and, then copy the characters to the new char*.
C++ strings are much more like strings of other languages.
char* are not true strings... they are just pointers to arrays of characters.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
March 5th, 2006, 03:01 AM
#4
Re: Isn't string equals to const char *?
 Originally Posted by sawer
I am reading some documents about c++ and it says:In c++ string is equals to const char *.
May I recommend throwing that document away .
What it probably meant to say is: In C, strings are represented by const char*.
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
|