473,587 Members | 2,263 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 10867
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
9389
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
3561
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
2477
by: Gil | last post by:
I have a string : string A = "2"; I need to get it's integer value : // ?? int val = A ; // ??
9
10265
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
15900
by: Torben Laursen | last post by:
Hi Can anyone show me how to convert between VARIANT and std::string and back, thanks! Torben ---
2
1922
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
8808
by: Jae | last post by:
Real(const string &fileName) { FILE * myInputFile = fopen(fileName, "rt"); ..... fclose(myInputFile);
9
2726
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
6494
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
8205
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. ...
1
7967
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
8220
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
6619
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
5712
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
5392
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...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2347
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1452
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.