Concat strings?

Started by
3 comments, last by FxMazter 18 years, 4 months ago
Hi! It has been a while since I did some c++ :) If I remember correctly I could concatenate two strings like this: #define STR1 "Hello" #define STR2 " World" STR1 "Want to concatenate with" STR2 So that I can write something like this: STR1\@STR2 which would equal to "Hello World" where \@ is the symbol for concatenation I don't want to create a string object for this... Thx!
Advertisement
Create a string large enough to hold both of them and then assign "hello" to the first and use strcat.

or

Make them both std::strings and just use the += operator.
In your specific case [where you are using string literals], you don't need any symbol. STR1 STR2 will result in "Hello World" as you would expect.

This fails if either of STR1 and STR2 are variables, of course.

CM
I'm not exactly sure what you're trying to do, but in C++ if you have two string literals seperated by nothing but whitespace, they will automatically be concatenated, so STR1 STR2 will work fine ("Hello" " World"). #defines probably aren't such a good idea for this. A const std::string object would probably get optimized out by the compiler to be as though it were a literal.
ok, thx!

This topic is closed to new replies.

Advertisement