سؤال

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?

هل كانت مفيدة؟

المحلول

c is a copy. If you wrote

char &c = someString[2];

then it would have been a reference.

نصائح أخرى

char c = someString[2];

copies the character at position 2 into 'c'

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top