Jump to content

  • Log In with Google      Sign In   
  • Create Account


string vs string literals.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 markrodgers11   Members   -  Reputation: 149

Like
1Likes
Like

Posted 06 October 2012 - 11:47 PM

I am currently reading a book called "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).


Edited by markrodgers11, 07 October 2012 - 12:03 AM.


Sponsor:

#2 Hodgman   Moderators   -  Reputation: 27454

Like
9Likes
Like

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.
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


#3 Servant of the Lord   Crossbones+   -  Reputation: 17027

Like
9Likes
Like

Posted 07 October 2012 - 01:53 PM

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.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

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.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal


#4 markrodgers11   Members   -  Reputation: 149

Like
1Likes
Like

Posted 08 October 2012 - 05:57 PM

hmm. alright that was explained perfectly! Thanks! :D




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS