C++:如何复制作为引用返回的字符?

发布于 2024-12-16 02:46:31 字数 192 浏览 4 评论 0原文

如果我这样做,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最冷一天 2024-12-23 02:46:31

c 是一个副本。如果你写了

char &c = someString[2];

,那就可以作为参考。

c is a copy. If you wrote

char &c = someString[2];

then it would have been a reference.

命比纸薄 2024-12-23 02:46:31
char c = someString[2];

将位置 2 处的字符复制到 'c'

char c = someString[2];

copies the character at position 2 into 'c'

她说她爱他 2024-12-23 02:46:31

它是独立的,因为 c 不是引用类型 - 它是 char,而不是 char &

由于它是值类型,因此执行复制。

It's independent because c is not a reference type - it is char, not char &.

A consequent of it being a value type, a copy is thus performed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文