473,405 Members | 2,171 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,405 software developers and data experts.

concatenating strings & string literals

i tried to output these 2 to the std. output:

const std::string hello = "Hello";
const std::string message = hello + ", world" + "!";

const std::string exclam = "!";
const std::string message2 = "hello" + " world" + exclam;
the first one runs fine but 2nd does not as we can not combine 2
string literals. i know that the "+" operator is left-associative,
hence:

/hello + ", world" + "!"/

means

/(hello + ",world") + "!"/

but what does /(hello + ", world")/ will produce:

a string literal
a character literal
or something else ?

Feb 8 '07 #1
8 2928
arnuld a écrit :
i tried to output these 2 to the std. output:

const std::string hello = "Hello";
const std::string message = hello + ", world" + "!";

const std::string exclam = "!";
const std::string message2 = "hello" + " world" + exclam;
the first one runs fine but 2nd does not as we can not combine 2
string literals. i know that the "+" operator is left-associative,
hence:

/hello + ", world" + "!"/

means

/(hello + ",world") + "!"/

but what does /(hello + ", world")/ will produce:
a string literal
a character literal
or something else ?

It will produce a temporary std::string object.
The function called is
std::string operator+(const std::string& lhs, const char* rhs);

So what your line do is in fact:
message = operator+( operator+( hello , ", world") , "!" );

Michael
Feb 8 '07 #2
arnuld wrote:
i tried to output these 2 to the std. output:

const std::string hello = "Hello";
const std::string message = hello + ", world" + "!";

const std::string exclam = "!";
const std::string message2 = "hello" + " world" + exclam;
the first one runs fine but 2nd does not as we can not combine 2
string literals. i know that the "+" operator is left-associative,
hence:

/hello + ", world" + "!"/

means

/(hello + ",world") + "!"/

but what does /(hello + ", world")/ will produce:

a string literal
a character literal
or something else ?
It is probably worth noting that adjacent string literals are
automatically concatenated. So you could write the second example as:

const std::string exclam = "!";
const std::string message2 = "hello" " world" + exclam;

Of course, you can usually just concatenate such literals yourself, but
sometimes this is convenient (e.g. breaking a literal across multiple
lines of source code).

--
Alan Johnson
Feb 8 '07 #3

"arnuld" <ge*********@gmail.comschrieb im Newsbeitrag
news:11**********************@l53g2000cwa.googlegr oups.com...
>i tried to output these 2 to the std. output:

const std::string hello = "Hello";
const std::string message = hello + ", world" + "!";

const std::string exclam = "!";
const std::string message2 = "hello" + " world" + exclam;
the first one runs fine but 2nd does not as we can not combine 2
string literals. i know that the "+" operator is left-associative,
hence:

/hello + ", world" + "!"/

means

/(hello + ",world") + "!"/

but what does /(hello + ", world")/ will produce:

a string literal
a character literal
or something else ?
The two literals in the 2nd example are internally represented as pointers
to char. So what you do is pointer arithmetics on char pointers, and then
you concatenate the resulting pointer with exclam using

std::string operator +( const char* lhs, const std::string& rhs )

Georg
Feb 8 '07 #4
const std::string hello = "Hello";
const std::string message = hello + ", world" + "!";
const std::string exclam = "!";
const std::string message2 = "hello" + " world" + exclam;
The two literals in the 2nd example are internally represented as pointers
to char.

i did not know that but i understand when you said that
So what you do is pointer arithmetics on char pointers, and then
you concatenate the resulting pointer with exclam using
std::string operator +( const char* lhs, const std::string& rhs )
out of my head
George, i guess, you have learnt C

?

-- arnuld

Feb 8 '07 #5
* arnuld:
i tried to output these 2 to the std. output:

const std::string hello = "Hello";
const std::string message = hello + ", world" + "!";

const std::string exclam = "!";
const std::string message2 = "hello" + " world" + exclam;
the first one runs fine but 2nd does not as we can not combine 2
string literals. i know that the "+" operator is left-associative,
hence:

/hello + ", world" + "!"/

means

/(hello + ",world") + "!"/

but what does /(hello + ", world")/ will produce:

a string literal
a character literal
or something else ?
Just try to compile it.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 8 '07 #6
Just try to compile it.
i compiled it before posting here. it runs without any error. 2nd one
gives an error, which is obvious as one can *not* combine "string
literals".

BTW, why you asked about compilation?

i never post anything without compiling 1st (& then thinking it over,
if it produces errors). only after that i post something here.

Feb 8 '07 #7
* arnuld:
>Just try to compile it.


i compiled it before posting here. it runs without any error. 2nd one
gives an error, which is obvious as one can *not* combine "string
literals".

BTW, why you asked about compilation?
Because you posted code that shouldn't compile.

i never post anything without compiling 1st (& then thinking it over,
if it produces errors). only after that i post something here.
Then it seems your question is not about the two first code example, but
literally what you wrote, 'what does /(hello + ", world")/ will
produce'. And here you combine a std::string and a string literal via
the + operator. If you have included <string>, then you have an
overload of that operator that takes these args and produces a std::string.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 8 '07 #8
On Feb 8, 8:29 pm, "Georg Krichel" <s...@no.spamwrote:
The two literals in the 2nd example are internally represented as pointers
to char. So what you do is pointer arithmetics on char pointers,
Actually they are arrays of char, and are represented
as such. They are not converted into pointers until you
use the '+' operator on them.

Feb 8 '07 #9

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

Similar topics

4
by: Juan | last post by:
Does any one know if there are reported bugs when concatenating strings? When debugging each variable has the correct value but when I try to concatenate them some values are missing (I can´t see...
4
by: FB's .NET Dev PC | last post by:
Interesting note, the code below as is will attempt to cast what is clearly indicated as a string into a double. This is becuase the use of + as a concatenation operator. The error message...
14
by: ranjmis | last post by:
Hi all, Below is the code wherein I am initializing double dimentional array inside main with string literals. Now I want to display the strings using a function call to which I just want to...
89
by: scroopy | last post by:
Hi, I've always used std::string but I'm having to use a 3rd party library that returns const char*s. Given: char* pString1 = "Blah "; const char* pString2 = "Blah Blah"; How do I append...
21
by: c | last post by:
Hi everybody. I'm working on converting a program wriiten on perl to C, and facing a problem with concatenate strings. Now here is a small program that descripe the problem, if you help me to...
3
by: James | last post by:
What's the difference between += and &= with reference to strings? Also, what are string literals? Thanks. And what are fixed lenth strings? and how do you declare them? Thanks
3
by: TheCoder | last post by:
Why does this work: It seems that we're concatenating two literals: std::cout << greeting + name + " Hello " + "There" << std::endl; but if you remove (greeting + name +), it doesn't work ?
74
by: cman | last post by:
Can you "walk across" C strings or char pointers (using *(sz+1)) like you can with arrays. If not, why not? If so, how? cman
20
by: Win Sock | last post by:
Hi All, somebody told me this morning that the following is leagal. char *a = "Hello wrold"; The memory is automatically allocated on the fly. Is this correct?
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
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,...
0
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,...
0
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...
0
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...
0
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,...
0
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...

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.