Jump to content
  • Advertisement
Sign in to follow this  

String Confusion !!

This topic is 4358 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, In my C++ book, im learning about the STL, vectors, and algorithms, and I came across an exaple which confused me. It may seem a little dumb, but Im confused. The book states (Beginning C++ Game Programming): - "Figure 4.3 shows an abstract view of the iterator returned by a call to inventory.begin().(Note that the figure is abstract because the vector 'inventory' doesnt contain the string literals "sword", "armor", and "shield"; it contains string objects.)" Figure 4.3:

               Inventory
    --------------------------------
    | "Sword" | "Armor" | "Shield" |
    --------------------------------
        ^
  inventory.begin()

Problem: - What does it mean by, "Note that the figure is abstract because the vector 'inventory' doesnt contain the string literals "sword", "armor", and "shield"; it contains string objects.)"? I dont quite get what it means by not string literals, there string objects. - Thanks for any help/advice.

Share this post


Link to post
Share on other sites
Advertisement
From what you posted above, the note would seem to mean that the strings are just of the String class, instead of a C-style string literal. (Ie. 'string itemname' instead of 'char *itemname'.)

Share this post


Link to post
Share on other sites
it is essentially clarifying that this is the declaration:

std::vector<std::string> inventory;

NOT

std::vector<const char *> inventory;


Since strings within quotes like "sword" are actually string literals (const char *) in C++ code. It just wanted to clarify that inventory contains std::strings which hold the word sword/armor/shield, despite the diagram using quotes.

Share this post


Link to post
Share on other sites

With string literals he means the 'raw' strings (e.g. a sequence of bytes in memory).
For convenience (and proper abstraction), you don't use these directly in most cases, but have a convenient string class providing utility functions, memory managment etc.
Objects of this class are of course not the same as an equivalent string literal, even though both can be used to carry strings.

Share this post


Link to post
Share on other sites
A "string literal" is a specific kind of instance of a char*: one that points at a compile-time constant string that you created in your code by wrapping it in double quotes.

A "string object" is an instance of the standard library class std::string.

BTW, so far, so good for this book; I may have to check it out ;)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!