string vs string literals.

Started by
2 comments, last by markrodgers11 11 years, 6 months ago
I am currently reading a book called "[color=#b22222]Accelerated C++ Practical Programming by Example" by Andrew Koenig and Barbara E. Moo and am confused on the difference between a string and a string literal.

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).
[/quote]
Advertisement
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.
So: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
A 'literal' is a data value that appears directly in your code.
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.

Think of a literal as a 'unnamed variable'.
Instead of doing this:
int myInt = myOtherInt;

You instead do this:
int myInt = 357; //357 = Sort-of like an unnamed variable.

The variable type of the literal depends on what symbols are with the variable.
Examples:
357 = some 'int'-like type.
true = a bool
false = a bool
357.0 = a double
357.0f = a float


There are others as well. A quick google pulls up this page listing many of them.

Here's another literal you've probably seen before:
char myChar = 'A'; //Single quotes on their own equals 'char' literal.

Then you see this type:
"my string"

Double quotes equals a string literal... This is of type char* or char[], I forget which.
A string literal produces what's called a 'C-style' string. A C-style string is an array of chars that end with a null character (so you know where it stops).

A string literal does not produce a std::string. std::string is a class that handles all the messiness of C-style strings for you, and was added by C++.
std::string is not part of the C++ language, but part of the C++ Standard Library that usually comes alongside the language.

std::strings can have C-strings assigned to them, and the C-string gets converted by the std::string to whatever it uses internally (It actually uses a C-string internally, but it wouldn't necessarily have to if it was designed differently).
char *myCString = "String literal";
std::string myString = myCString;

Or:
std::string myString ="String literal";

Two c-strings cannot be concatenated together. Example:
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!


However, std::strings DO allow concatenation.
std::string strA = "A";
std::string strB = "B";
std::string myStr = strA + strB + "string literal";
myStr += "more text";


As an aside, the newest version of the C++ standard (standardized late last year) allows users to create their own literal types, so we'll eventually be able to create our own literals like:
Weight myWeight = 110_lbs; //Where 'Weight' is a custom made type, and '_lbs' is a user-defined literal.
hmm. alright that was explained perfectly! Thanks! :D

This topic is closed to new replies.

Advertisement