Here is the excerpt from what I read:
The second new idea is that we can use + to concatenate a string and a string literal--or, for that matter, two strings (but not two string literals).
Edited by markrodgers11, 07 October 2012 - 12:03 AM.
Posted 06 October 2012 - 11:47 PM
The second new idea is that we can use + to concatenate a string and a string literal--or, for that matter, two strings (but not two string literals).
Edited by markrodgers11, 07 October 2012 - 12:03 AM.
Posted 07 October 2012 - 12:16 AM
std::string String = "String literal";the variable called "String" (of type std::string) is a string, the actual text inside quotes is a string literal.
std::string a = "a";
std::string b = "b";
std::string c = a+b;//concatenate two strings
c = a+"literal";//concatenate 1 string and 1 literal
c = "foo" + "bar";//concatenate 2 literals -- this doesn't work, it hopefully will give you a compile error.
c = std::string("foo") + "bar";//turning one of them into a temporary string does make it work
Posted 07 October 2012 - 01:53 PM
int myInt = 357; //357 = a number literal. int myInt = #00FFAA; //#00FFAA = a number literal, but in hexadecimal format. std::string myString = "Hello!"; //"Hello!" = a string literal. float myFloat = 357.0f; //A float literal. The 'f' means 'float'. double myDouble = 357.0; //A double literal. Any decimal number is a double by default, I think.
double myDouble = 357.0f; //A float literal (notice the 'f'). It'll get converted to a double during the assignment.
int myInt = myOtherInt;
int myInt = 357; //357 = Sort-of like an unnamed variable.
357 = some 'int'-like type. true = a bool false = a bool 357.0 = a double 357.0f = a float
char myChar = 'A'; //Single quotes on their own equals 'char' literal.
"my string"
char *myCString = "String literal"; std::string myString = myCString;Or:
std::string myString ="String literal";
char *myCString = "String literal A" + "String literal B"; //Won't work!Or:
char *myCStringA = "text"; char *myCStringB = "text"; char *myCStringC = myCStringA + myCStringB; //Won't work!
std::string strA = "A"; std::string strB = "B"; std::string myStr = strA + strB + "string literal"; myStr += "more text";
Weight myWeight = 110_lbs; //Where 'Weight' is a custom made type, and '_lbs' is a user-defined literal.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal