Implicit type conversion between strings and char *

U

utab

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,
 
V

Victor Bazarov

utab said:
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
 
G

Gianni Mariani

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::eek:perator ==.

It need not perform a conversion to compare equality, perhaps it does,
but it does not need to.
 
J

Juha Nieminen

utab said:
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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top