简体   繁体   English

C ++,字符串和指针

[英]C++, strings, and pointers

I know this is rudimentary but I know nothing about C++. 我知道这很简陋但我对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? 为了在稍后的同一代码块中引用str? If so, what type of error would the latter produce? 如果是这样,后者会产生什么类型的错误?

That depends entirely on the return type of getNextRequest . 这完全取决于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. 字符串可以在它们声明的范围内使用和重用。它们基本上包含一个可变的C字符串和一些处理信息,辅助方法等。

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. 然后可以正常使用该字符串(此处为str ),而无需担心使用范围外的本地等。

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. 有时候需要一个指向字符串的指针,但除非你将它用作out参数,否则这些指针很少见并且表明某些设计有些奇怪。

Which you use depends on what getNextRequest() returns. 您使用哪个取决于getNextRequest()返回的内容。 If it returns a string * , then use the first line, if it returns string then use the second. 如果它返回一个string * ,那么使用第一行,如果它返回string则使用第二行。

So if the declaration of getNextRequest is like this: 所以如果getNextRequest的声明是这样的:

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 . 将创建getNextRequest返回的stringgetNextRequest 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. 如果要更改str的内容并希望这些更改也在getNextRequest返回的字符串中, getNextRequest必须返回指针或引用。

If this is what you want, then you should define getNextRequest as: 如果这是你想要的,那么你应该将getNextRequest定义为:

string& getNextRequest()

and use it like: 并使用它像:

string& str = getNextRequest();

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

As noted by @dasblinkenlight, that would be a syntax error 正如@dasblinkenlight所指出的那样,这将是一个语法错误

But to answer your original question, is it necessary? 但要回答你原来的问题,是否有必要? No. In general, you should not use pointers unless you must. 不可以。一般情况下,除非必须,否则不应使用指针。

Especially with the STL. 特别是STL。 The STL is not designed to be used with pointers--it does dynamic memory management for you. STL不是设计用于指针 - 它为您进行动态内存管理。 Unless you have a good reason, you should always use vector<int> v and string s rather than vector<int>* or string* . 除非你有充分的理由,否则你应该总是使用vector<int> vstring s而不是vector<int>*string*

You will probably need to provide a little bit more information regarding this function getNextRequest(). 您可能需要提供有关此函数getNextRequest()的更多信息。 Where is it from? 这个从哪里来? Library? 图书馆? API? API? Purpose? 目的?

If the return type of the function is a string* (pointer to str), then the string has been allocated to the "heap". 如果函数的返回类型是字符串*(指向str的指针),则该字符串已分配给“堆”。 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. 如果函数的返回类型只是一个字符串(意思不是指针),它将返回值,而不是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. 在这种情况下,变量将在堆栈上分配,并且只能在代码块的范围内引用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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