[英]std::string getting (char *) instead of (const char *)
std::string.c_str()
returns a (const char *) value. std::string.c_str()
返回一个(const char *)值。 I Googled and found that I can do the following: 我用Google搜索,发现我可以执行以下操作:
std::string myString = "Hello World";
char *buf = &myString[0];
How is this possible? 这怎么可能?
&myString[0]
is an object of type std::string
, so how can this work? &myString[0]
是std::string
类型的对象,那么它如何工作?
&myString[0]
is a object of typestd::string
&myString[0]
是std::string
类型的对象
No it isn't. 不,不是。
myString[0]
is a reference to the first character of the string; myString[0]
是对字符串的第一个字符的引用; &myString[0]
is a pointer to that character. &myString[0]
是指向该字符的指针。 The operator precedence is such that it means &(myString[0])
and not (&mystring)[0]
. 运算符优先级是指
&(myString[0])
而不是(&mystring)[0]
。
Beware that, accessed this way, there's no guarantee that the string will be zero-terminated; 请注意,以这种方式访问,不能保证字符串将被零终止; so if you use this in a C-style function that expects a zero-terminated string, then you'll be relying on undefined behaviour.
因此,如果您在期望零终止字符串的C样式函数中使用它,那么您将依赖于未定义的行为。
There are const
and non- const
overloads of std::string::operator[]( size_type pos )
, and the non-const version returns a char&
, so you can do things like std::string::operator[]( size_type pos )
有const
和非const
重载,非const版本返回char&
,所以你可以做类似的事情
std::string s("Hello");
s[0] = 'Y';
Note that, since s[0]
returns char&
, then &s[0]
is the address of element s[0]
. 注意,由于
s[0]
返回char&
,因此&s[0]
是元素s[0]
的地址。 You can assign that address to a char*
. 您可以将该地址分配给
char*
。 It is up to you not to misuse this pointer. 你不要滥用这个指针。
It has to do with operator precedence . 它与运算符优先级有关 。 The
[]
operator has higher precedence than the address-of operator &
, so the address-of operator works on the character reference returned by the strings operator[]
function. []
运算符的优先级高于address-of运算符&
,因此address-of运算符适用于strings operator[]
函数返回的字符引用。
The operator []
( std::string::char& operator[] (size_t pos) ) overloaded returns a reference to the character at the index. operator
[]
( std :: string :: char&operator [](size_t pos) )重载返回对索引处字符的引用。 You are taking the address of such character reference which is fine. 您正在使用此类字符引用的地址,这很好。
So, myString[0]
return type is not std::string
but char&
. 所以,
myString[0]
返回类型不是std::string
而是char&
。
There is no reason to do it. 没有理由这样做。 You can directly do -
你可以直接做 -
myString[0] = 'h';
The std::string methods c_str()
and operator[]
are two diferent methods , which return two different types. std :: string方法
c_str()
和operator[]
是两种不同的方法 ,它们返回两种不同的类型。
The method c_str() does indeed return a const char*
. 方法c_str()确实返回了一个
const char*
。
const char* c_str() const;
However, the method operator[] returns instead a reference to a char. 但是,方法operator []返回对char的引用。 When you take the address of it, you get the address of a char.
当你获取它的地址时,你得到一个字符的地址。
char& operator[] (size_t pos);
const char& operator[] (size_t pos) const;
You are wrong. 你错了。
&myString[0]
is not of type std::string
, it is of type char *
. &myString[0]
不是std::string
类型,它的类型为char *
。
The []
operator has higher precedence and operator[]
returns a reference to char
, and its address (the &
operator) is of type char *
. []
运算符具有更高的优先级, operator[]
返回对char
的引用,其地址( &
运算符)的类型为char *
。
The std::string
type has an operator[]
that allows indexing each one of the characters in the string. std::string
类型有一个operator[]
,允许索引字符串中的每个字符。 The expression myString[0]
is a (modifiable) reference to the first character of the string. 表达式
myString[0]
是对字符串的第一个字符的(可修改的)引用。 If you take the address of that you will get a pointer to the first character in the array. 如果你取了那个地址,你会得到一个指向数组中第一个字符的指针。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.