473,320 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 4580
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Siemel Naran | last post by:
Here is a question about implicit conversion from T (&) to ptrcarray<T>. I wrote a class template <class T> struct ptrcarray { T * array; size_t size;
15
by: buda | last post by:
Let me see if I got this :) 1. I know the rules for type conversions in arithmetic expressions 2. I know that an implicit type conversion is done at assignment, so float x = 1.23; int t = (int)...
8
by: Roger Leigh | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 A lot of functions use const pointer arguments. If I have a non-const pointer, it is transparently made const when I pass it to the function, e.g....
11
by: Steve Gough | last post by:
Could anyone please help me to understand what is happening here? The commented line produces an error, which is what I expected given that there is no conversion defined from type double to type...
11
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
2
by: tim | last post by:
We've started to use a coding standards checker at work. The following code results in a warning: 38: CPlainText::CPlainText(const char *szPath,bool bTimeStamp,bool bSaveLast) 39: ...
20
by: srinivasarao_moturu | last post by:
template<class TClass ABC { public : void SetValue(T k) { val = k; } private: T val; };
1
by: jschon | last post by:
How do I compile with g++ version 4.0 so that a int will not be implcitly converted to a string. for example...i would like the following code to result in an error int x = 4; string cat;...
17
by: arnuld | last post by:
I have checked the FAQ: http://c-faq.com/malloc/mallocnocast.html FAQ discusses a special case when programmer has forgotten to do #include <stdlib.h>. I am including this header and I am not...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.