I play with C ++ links and notice a bit strange behavior that I cannot explain. I understand that if I have a non-constant variable and a constant refers to the same variable, and then change the non-constant variable, the link should reflect this modification.
Example:
void foo() { int x = 5; const int& y = x; x = 10; std::cout << "x = " << x << std::endl; std::cout << "y = " << y << std::endl; }
produces the following output for me:
x = 10 y = 10
However, if I change the type to std :: string, the const reference does not reflect the changed variable:
void foo() { std::string x = "abc"; const std::string& y = x; x = "xyz"; std::cout << "x = " << x << std::endl; std::cout << "y = " << y << std::endl; }
produces the following for me:
x = xyz y = abc
Is this the usual expected behavior when trying to do this with std :: string? (I am using GCC 4.6.0, I don't have another compiler at the moment, so I donโt know if this only happens with this particular version or not.)
source share