Jump to content

View more

Image of the Day

Isn't this a lovely apple tempart placeholder thing  #gamedev worth a #screenshotsaturday even I would say. https://t.co/fQH1d0ySIG
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

string vs string literals.

2: Adsense

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   


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.

#2 Hodgman   Moderators   


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

#3 Servant of the Lord   Members   


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.
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;
std::string myString ="String literal";

Two c-strings cannot be concatenated together. Example:
char *myCString = "String literal A" + "String literal B"; //Won't work!
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' or 'SotL' rather than copy+pasting it all the time.
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 | [Fly with me on Twitter]

#4 markrodgers11   Members   


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.