String Confusion !!

Started by
3 comments, last by Zahlman 17 years, 10 months ago
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.
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'.)
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.

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

This topic is closed to new replies.

Advertisement