By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,203 Members | 1,549 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,203 IT Pros & Developers. It's quick & easy.

Implicit type conversion between strings and char *

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.