简体   繁体   English

const引用返回的stl字符串与const char *

[英]stl string return by const reference vs. const char*

i have a STL string member variable in my class, 我在课堂上有一个STL字符串成员变量,

class A {
public:
   const char* getData1() const { return data.c_str());
   const string& getData2() const { return _data; }
private:
   string _data;
};

getData1() vs. getData2() , which one is better? getData1()getData2()哪个更好?

The one returning a string reference is the better of the two choices. 返回字符串引用的是这两种选择中较好的一种。 If the string is changed internally, a previously returned string reference would still be valid, but the const char * probably would not be. 如果在内部更改了字符串,则先前返回的字符串引用仍然有效,但是const char *可能无效。

Better than either of those choices may be to just return by value. 比这些选择中的任何一个更好的是仅按价值返回。

std::string getData3() const { return data_; }

Also, I'd suggest not prefixing variables with an underscore, and get rid of that using namespace std; 另外,我建议不要在变量前加下划线,并using namespace std;消除该前缀using namespace std; statement. 声明。

Both are a little dangerous because your object may go out of scope, or its memory may be freed. 两者都有些危险,因为您的对象可能超出范围,或者可能释放了其内存。 But you'll still have a pointer or reference to one of its members. 但是您仍将具有指向其成员之一的指针或引用。

If you really want to do this though, it just depends on if you want access to the string methods or not. 如果您确实要执行此操作,则仅取决于您是否要访问字符串方法。 Also c_str() 's const char* may be memory that will be freed on you. 同样, c_str()const char*可能是将在您身上释放的内存。

If _data is not that big maybe just return a string , not a const string& . 如果_data没那么大,也许只返回一个string ,而不是const string&

Generally I prefer getData2() . 通常我更喜欢getData2() The const char* returned by c_str() method can only be used as long as data 's non-const methods are not called. 只要不调用data的非const方法,就只能使用c_str()方法返回的const char* After that, the content of const char* is undefined. 之后, const char*的内容未定义。 By returning const char* , you are masking this issue. 通过返回const char* ,可以掩盖此问题。 If the user of the class needs to use a C-style string, he can call c_str() himself on the returned const string& . 如果该类的用户需要使用C样式的字符串,则可以自己对返回的const string&调用c_str()

Here we are also assuming that the user of getData2() reasonably assumes that the returned reference will only available to him as long as the object of class A is not destroyed yet. 在这里,我们还假设getData2()的用户合理地假定返回的引用仅对他可用,只要该类A的对象尚未销毁即可。

Returning a const string& is better. 返回一个const string&更好。 If you need a const char* , you can just call getData2().c_str() which is practically identical to what getData1() does. 如果需要const char* ,则可以只调用getData2().c_str() ,它实际上与getData1()作用相同。

Remember std::string s are better than const char* strings because they store the length, instead of being null terminated. 请记住, std::string优于const char*字符串,因为它们存储长度,而不是以null终止。 This means if you want to get the length of that string, strlen(getData1()) is an O(n) operation, but getData2().length() is an O(1) operation. 这意味着,如果要获取该字符串的长度, strlen(getData1())是O(n)操作,而getData2().length()是O(1)操作。

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

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