471,319 Members | 1,633 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

Implicit type conversion between strings and char *

Dear all,

I was trying to write a more complex program, and while searching for
sth in my reference C++ primer, by Lippman. I had a question in the
mind, see the code below

#include <string>
#include <iostream>
#include <cstring>
int main(){
const char *lit="hello";
std::string str("hello"); // implicit conversion from const char*
to string,right?
if(str=="hello") // implicit conversion from const
char* to string, right?
std::cout << "the same";
else
std::cout<< "different";
return 0;
}

To be able to compare, one type should be converted to the other. I
could not find the answer in the book. Maybe I missed :-)

Thanks,

Jun 22 '07 #1
3 4360
utab wrote:
I was trying to write a more complex program, and while searching for
sth in my reference C++ primer, by Lippman. I had a question in the
mind, see the code below

#include <string>
#include <iostream>
#include <cstring>
int main(){
const char *lit="hello";
std::string str("hello"); // implicit conversion from const char*
to string,right?
if(str=="hello") // implicit conversion from const
char* to string, right?
std::cout << "the same";
else
std::cout<< "different";
return 0;
}

To be able to compare, one type should be converted to the other. I
could not find the answer in the book. Maybe I missed :-)
It depends on the type. Here are two examples:

struct HasConversion {
HasConversion(int) {}
bool operator ==(HasConvesion const&) { return false; }
};

struct HasComparisonInstead {
bool operator ==(int) { return false; }
};

int main() {
HasConversion hc(42);
hc == 666;
HasComparisonInstead hci;
hci == 42;
}

In fact, there is an operator == defined for comparing 'std::string'
with a char const*. So, no implicit conversion should happen in the
latter case (from your posting). As for the creation of the string
in the former case, the conversion is not "implicit". It's just
parameterised construction.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 22 '07 #2
utab wrote:
....
To be able to compare, one type should be converted to the other. I
could not find the answer in the book. Maybe I missed :-)
str is a std::string object (which is a std::basic_string<char>).

It is initialized with the constructor that takes a <const char *>.

It is compared using the std::string::operator ==.

It need not perform a conversion to compare equality, perhaps it does,
but it does not need to.
Jun 22 '07 #3
utab wrote:
std::string str("hello"); // implicit conversion from const char*
to string,right?
There's no implicit conversion. std::string has a constructor which
takes a const char* as parameter. This is an explicit constructor
behavior.
if(str=="hello") // implicit conversion from const
char* to string, right?
AFAIK std::string has an operator==(const char*) defined, so the
comparison is explicit. (In fact, I don't know if the C++ standard
requires for std::string to have such an explicit comparison
operator, but I bet that even if it didn't, all compiler implementations
define it anyways, for efficiency reasons.)
Jun 23 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Siemel Naran | last post: by
15 posts views Thread by buda | last post: by
8 posts views Thread by Roger Leigh | last post: by
11 posts views Thread by Steve Gough | last post: by
2 posts views Thread by tim | last post: by
20 posts views Thread by srinivasarao_moturu | last post: by
1 post views Thread by jschon | last post: by
17 posts views Thread by arnuld | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.