C++:如何复制作为引用返回的字符?
如果我这样做,
string someString = "hello";
char c = someString[2];
c
变量是否引用 someString
内的字符,或者它本身是一个新的独立字符?
如果不是独立的,如何复制呢?
If I do
string someString = "hello";
char c = someString[2];
does the c
variable refer to a character inside someString
, or is it a new independent char on it's own?
If it is not independent, how do I copy it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
c
是一个副本。如果你写了,那就可以作为参考。
c
is a copy. If you wrotethen it would have been a reference.
将位置 2 处的字符复制到 'c'
copies the character at position 2 into 'c'
它是独立的,因为
c
不是引用类型 - 它是char
,而不是char &
。由于它是值类型,因此执行复制。
It's independent because
c
is not a reference type - it ischar
, notchar &
.A consequent of it being a value type, a copy is thus performed.