🔒 Locked
std::string problem
Advertisement
Original Post
In your code example, "abc" is a string litteral and "def" is also a string litteral. You can't add string litterals like this - it doesn't work this way. You can add string objects (i order to concatanate them) but not string litterals. You can also add a string litteral to a string object.
What you want to do is
Or more simply
The second solution works because it first creates a string object using the string litteral, then you add a string litteral to this string object.
HTH,
What you want to do is
std::string s = std::string("abc") + std::string("def");Or more simply
std::string s = std::string("abc") + "def";The second solution works because it first creates a string object using the string litteral, then you add a string litteral to this string object.
HTH,
Also, just to be complete.
You CAN concatanate string litterals, but the syntax is a little different:
There is no operator needed for this.
This kind of syntax is very useful, when you keep your string litterals as defines:
So now, if you ever need to change first name - you only have to do it in one place. All of the rest defines will take it's value.
You CAN concatanate string litterals, but the syntax is a little different:
std::string blah = "abc" "def";There is no operator needed for this.
This kind of syntax is very useful, when you keep your string litterals as defines:
#define FIRSTNAME "John"#define LASTNAME "Doe"#define FULLNAME FIRSTNAME " " LASTNAME#define EMAIL FIRSTNAME "." LASTNAME "@gmail.com"So now, if you ever need to change first name - you only have to do it in one place. All of the rest defines will take it's value.
Quote:
Original post by dalleboy This is why defines r0XorZ... ;)
No they don't - you can't put them into namespaces or classes, you can't limit their scope. That is why they are bad. BAD! :)
Anyway, the same thing with const std::string is also possible (and personaly, i'd prefer this method):
const std::string FIRSTNAME ("John");const std::string LASTNAME ("Doe");const std::string FULLNAME (FIRSTNAME + " " + LASTNAME);const std::string EMAIL (FIRSTNAME + "." + LASTNAME + "@gmail.com");I posted the defines only to show why the concatenation of string litterals are useful.
Of course, in *real* projects you try to avoid putting too many string literals in your code in the first place, instead reading the important text from files or resources :)
Note that the "string literal concatenation" does something rather different. It's a compile-time operation, and *only* works with a literal for *both* parts. The compiler is simply parsing it as if it actually were one literal, and actually generates one literal stored in the executable when it emits the code. The feature is really intended more, AFAIK, to allow you to wrap string literals across lines of source code. But again, avoid string literals in a serious project :)
Note that the "string literal concatenation" does something rather different. It's a compile-time operation, and *only* works with a literal for *both* parts. The compiler is simply parsing it as if it actually were one literal, and actually generates one literal stored in the executable when it emits the code. The feature is really intended more, AFAIK, to allow you to wrap string literals across lines of source code. But again, avoid string literals in a serious project :)
Quote:
Original post by Zahlman
Of course, in *real* projects you try to avoid putting too many string literals in your code in the first place, instead reading the important text from files or resources :)
Not 100% true. If you are using something like GNU gettext, then you are putting ALL of your original strings as litterals in source code. *wink* ;)
Otherwise you're right.
Thanks for the help, everyone. Nice to see so many friendly people!
Now I have a new problem. I have a function that calculates two local variables. I want to put these two variables together into a std::string and return it.
e.g.
int x = 3;
int y = 7;
std::string s = x + y;
return s;
I want this to return a string like "37", but it seems to be treating the ints as ASCII values instead. How do I convert the ints into a std::string?
Now I have a new problem. I have a function that calculates two local variables. I want to put these two variables together into a std::string and return it.
e.g.
int x = 3;
int y = 7;
std::string s = x + y;
return s;
I want this to return a string like "37", but it seems to be treating the ints as ASCII values instead. How do I convert the ints into a std::string?
Quote:The easiest way is probably:
Original post by mike223
Thanks for the help, everyone. Nice to see so many friendly people!
Now I have a new problem. I have a function that calculates two local variables. I want to put these two variables together into a std::string and return it.
e.g.
int x = 3;
int y = 7;
std::string s = x + y;
return s;
I want this to return a string like "37", but it seems to be treating the ints as ASCII values instead. How do I convert the ints into a std::string?
std::string s = boost::lexical_cast<std::string>(x) + boost::lexical_cast<std::string>(y);You can also do the same thing 'manually' using std::stringstream (which is more or less what lexical_cast<> does internally).
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement