Jump to content
  • Advertisement
Sign in to follow this  
NUCLEAR RABBIT

String Question.

This topic is 4491 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, 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???

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.)

Share this post


Link to post
Share on other sites
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.)

Share this post


Link to post
Share on other sites
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].

Share this post


Link to post
Share on other sites
A string literal is the C++ syntax used to represent a string in source code.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!