简体   繁体   中英

C++, strings, and pointers

I know this is rudimentary but I know nothing about C++. Is it necessary to do:

string *str = getNextRequest();

rather than

string str = getNextRequest();

in order to reference str later on in the same block of code? If so, what type of error would the latter produce?

That depends entirely on the return type of getNextRequest .

Strings can be used and reused throughout the scope they're declared in. They essentially contain a mutable C string and some handling information, helper methods, etc.

You can, very safely, return a string from a function and the compiler will make a copy or move it as necessary. That string ( str here) can then be used normally, with no worries about using out-of-scope locals or such.

There are times when a pointer to a string is needed, but unless you're using it as an out parameter, those tend to be rare and indicate some design oddity.

Which you use depends on what getNextRequest() returns. If it returns a string * , then use the first line, if it returns string then use the second.

So if the declaration of getNextRequest is like this:

string getNextRequest();

Then

string str = getNextRequest();

is correct. If the declaration is like this:

string *getNextRequest();

Then you can go with

string *str = getNextRequest();

or

string str = *getNextRequest();
string str = getNextRequest();

will create a copy of the string returned by getNextRequest . If you want to alter the contents of str and wish that these changes are also within the string returned by getNextRequest you have to return a pointer or reference.

If this is what you want, then you should define getNextRequest as:

string& getNextRequest()

and use it like:

string& str = getNextRequest();

string str* = getNextRequest();

As noted by @dasblinkenlight, that would be a syntax error

But to answer your original question, is it necessary? No. In general, you should not use pointers unless you must.

Especially with the STL. The STL is not designed to be used with pointers--it does dynamic memory management for you. Unless you have a good reason, you should always use vector<int> v and string s rather than vector<int>* or string* .

You will probably need to provide a little bit more information regarding this function getNextRequest(). Where is it from? Library? API? Purpose?

If the return type of the function is a string* (pointer to str), then the string has been allocated to the "heap". This means, it does not matter which block of code you reference the string from. As long as you maintain the pointer, you will be able to access it.

If the return type of the function is simply a string (meaning not a pointer), it will return the value, not the address of str. In essence, you would be "copying" the string to your new variable. In this case, the variable would be allocated on the stack, and you would only be able to reference it when in the scope of the code block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM