String Question.

Started by
10 comments, last by NUCLEAR RABBIT 17 years, 9 months ago
Hello, I was reading throught my C++ book, and came across the words, "string literal". I know what a string is, but I dont get the difference between a string literal, and a string object. To me, they seem to be the samething, but Im pretty sure im absolutly wrong. I checked wikipedia, but there was no result for string object, as there was for string literal. String Literal:

cout << "This is a String!"

String Object:

string myString_object = "Hello"; // is this correct???

Advertisement
In your second snippet, the string object is constructed by assigning a string literal to it. The string literal is stored in const memory i believe and is raw data.

Dave
The difference is just how they're represented as data - a string literal is a const char *, while a string object is a class containing the string and other information and methods.
Quote:Original post by crusadingknight
The difference is just how they're represented as data - a string literal is a const char *, while a string object is a class containing the string and other information and methods.


So string objects are only in Classes?
No, the object is a class that is part of the C++ language. (If you don't know about classes yet, then don't really worry about the difference.)
In somewhat technical terms, a string literal is actually stored in the .exe file that you generate when you compile your program. When you execute the file, all of the file's data and code gets loaded into memory, including the data for any string literals. Whenever you use a string literal in your program (such as sending it to cout, or assigning it to a string object), what you're doing is basically saying, "Starting at this location in memory, there is an array of characters. Treat this array as a C-style string and do something with it." It's special because the array wasn't allocated by you, but essentially always existed within your program, from the moment it was loaded. And because of it's type of storage in memory, you shouldn't trying to alter it. (Assigning it to a string object, and the altering the object is fine, since all you did was read the string literal and make a copy of it; altering the copy is fine, since it is in memory that you allocated, through the help of the string object.)
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
This is my way of thinking about it (which may be technically wrong)

a literal is the actual information eg. "hi", the object(or variable) is just how it is stored :) "hi" will always be "hi" because that is (literally) what it is, but string_object can be "hello", "hi" or "" because it is just a way of identifying it [smile].
A string literal is the C++ syntax used to represent a string in source code.
A string literal is stored differently in memory (sort of) than a string object.

The difference in the example you provided is that tin he first example, "This is a string" gets stored in memory when you load the program. You can't ever change it (since it's a constant) and can't refer to it outside the exact statement you wrote it in.

In the second example, you are creating a string object. You can change it, until it goes out of scope, and refer to it multiple times. It takes up memory on the
"stack," which is a certain area of memory, while the string literal in the first example takes up memory in what can be called the "DATA" area of memory, which is set up when you load the program, and which cannot be changed at all once the program starts running.
my siteGenius is 1% inspiration and 99% perspiration
Thanks for the Explainations everybody. They really helped me have a better understanding of the two meanings. But just to make things crystal clear, the first example below is a string literal, and the 2nd is a string object because the first one you cannot change or reuse, but the second one you can??

1st:
cout << "Hello!";

2nd:
string omg;

This topic is closed to new replies.

Advertisement