473,385 Members | 1,409 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,385 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 4586
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
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...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

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.