473,572 Members | 3,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

perferred way of converting const char * to const std::string &

Just looking for opinion on which of the 3 methods below people use in
their code when they convert a 'const char *' to a 'const std::string
&'

came across #3 in someone's code and I had to think for a sec. At
first I read it as converting a 'const char *' to a 'std::string *'

void f(const std::string &s)
{
std::cout << s.size() << "\n";
}

int main()
{
const char *c="sal";

f(c); //1
f(std::string(c )); //2
f(static_cast<s td::string>(c)) ; //3

return 0;
}

Aug 9 '06 #1
2 10864
po************* *@yahoo.com wrote:
Just looking for opinion on which of the 3 methods below people use in
their code when they convert a 'const char *' to a 'const std::string
&'

came across #3 in someone's code and I had to think for a sec. At
first I read it as converting a 'const char *' to a 'std::string *'

void f(const std::string &s)
{
std::cout << s.size() << "\n";
}

int main()
{
const char *c="sal";

f(c); //1
f(std::string(c )); //2
f(static_cast<s td::string>(c)) ; //3

return 0;
}
(1) is the most common.

(2) is only needed when you have 'f' overloaded and must pick one with
'std::string' as its argument.

(3) is the same as (2), IMO.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 9 '06 #2
<po************ **@yahoo.comsch rieb im Newsbeitrag
news:11******** **************@ 75g2000cwc.goog legroups.com...
Just looking for opinion on which of the 3 methods below people use in
their code when they convert a 'const char *' to a 'const std::string
&'

came across #3 in someone's code and I had to think for a sec. At
first I read it as converting a 'const char *' to a 'std::string *'

void f(const std::string &s)
{
std::cout << s.size() << "\n";
}

int main()
{
const char *c="sal";

f(c); //1
f(std::string(c )); //2
f(static_cast<s td::string>(c)) ; //3

return 0;
}
If it works without a cast, you shouldn't use a cast. So (1) is probably the
best solution, not only because it's also the one with least typing. This
will also help you not to cast away errors, which may happen if you have to
change the type of the function's argument or the value passed to it latter.
In your example, that might be not likely to happen, but with other types,
like int's and long's it could cause problems.

Except for their syntax (2) and (3) do basically the same, so if you really
have to use a cast, use whichever looks better to you. There is less typing
with a function-style cast (2), but it is easier to find a static_cast in a
large program, especially with tools like grab. Also, a static_cast (or
other *_cast's) can be used for all types whereas function-style casts can
only be used for named types. For example, you can use static_cast<uns igned
int>(...) but you cannot use unsigned int(...) as a cast.

Regards
Heinz

Aug 9 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
9388
by: selder21 | last post by:
Hello, I have a class with constructor taking a const string&. Now i want to call this constructor with a string literal. Because this is of type char* there are overload resolution conflicts. If i make another constructor with parameter const char*, how can i call the constructor with the const string& ? I tried
7
3558
by: Sims | last post by:
Hi, if i have a code const char * GetValue() { std::string szVectorValue = ...// get a std::string from the vector return szVectorValue.c_str(); }
13
2468
by: Gil | last post by:
I have a string : string A = "2"; I need to get it's integer value : // ?? int val = A ; // ??
9
10263
by: Mathieu Malaterre | last post by:
Hello, This thread follow my previous one on the gcc mailing list. Basically I -still- have a problem in my code. I define in a header file: static const std::string foo = "bar"; Which not only seems to be a bad practice but also creates a bug on MacOSX panther (gcc 3.3).
5
15899
by: Torben Laursen | last post by:
Hi Can anyone show me how to convert between VARIANT and std::string and back, thanks! Torben ---
2
1919
by: anelma via .NET 247 | last post by:
Following code works fine, when compiled with VS 6.0, but not anymore when compiled in .NET. What's wrong here, I can't see it by myself? arrString content will be garbage with .net compilation, but when compiled with 6.0 it contains string from Vector (that's how I want it to work). std::vector<std::string> Vector; ... void...
5
8803
by: Jae | last post by:
Real(const string &fileName) { FILE * myInputFile = fopen(fileName, "rt"); ..... fclose(myInputFile);
9
2725
by: priyanka | last post by:
Hi there, I want to convert a String into integer. I get the string froma file using : string argNum; getline(inputStream,argNum); I now need to convert argNum into integer.
4
6492
by: Ramesh | last post by:
Hi, I need to convert unsigned long data to a std::string, I googled a bit to see if the string class supports any methods to achieve this - but didnt find any, I think of using sprintf and converting to a char buf and then putting it back in std::string - is anyone aware of any other way? Thanks
0
7733
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7957
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8155
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7705
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6337
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5524
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5248
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1240
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.