m**g 发帖数: 1 | 1 string a1 = "Hello";
string a2 = "world!";
string* s1 = 0;
string& s2 = a1;
s1 = &a1;
s2 = a2;
std::cout << *s1 << " " << s2 << std::endl;
and why? |
j*****k 发帖数: 1198 | 2
//after this line s1=s2
//so outcome is world! world!
Am I right?
【在 m**g 的大作中提到】 : string a1 = "Hello"; : string a2 = "world!"; : string* s1 = 0; : string& s2 = a1; : s1 = &a1; : s2 = a2; : std::cout << *s1 << " " << s2 << std::endl; : and why?
|
d*******d 发帖数: 2050 | 3 I think you are right.
【在 j*****k 的大作中提到】 : : //after this line s1=s2 : //so outcome is world! world! : Am I right?
|
d*******d 发帖数: 2050 | 4
s1是a1的地址,s2是a1的reference,不能说s1 = s2.
【在 j*****k 的大作中提到】 : : //after this line s1=s2 : //so outcome is world! world! : Am I right?
|
j*****k 发帖数: 1198 | 5 那后面s2=a2了,就不是s2变成a2的reference了?
还是说s2就只能是a1的reference, s2=a2, 就是把a1=a2了?
【在 d*******d 的大作中提到】 : : s1是a1的地址,s2是a1的reference,不能说s1 = s2.
|
d*******d 发帖数: 2050 | 6
是谁的reference只能在定义的时候决定,然后就不能改了。
对。
【在 j*****k 的大作中提到】 : 那后面s2=a2了,就不是s2变成a2的reference了? : 还是说s2就只能是a1的reference, s2=a2, 就是把a1=a2了?
|
j****g 发帖数: 597 | 7 s2 is the reference of a1.
so s2=a2 ==> a1=a2 ==> *s1="World!" |
c********x 发帖数: 84 | 8
the answer is : world! world!
【在 m**g 的大作中提到】 : string a1 = "Hello"; : string a2 = "world!"; : string* s1 = 0; : string& s2 = a1; : s1 = &a1; : s2 = a2; : std::cout << *s1 << " " << s2 << std::endl; : and why?
|