c++ - changing referent of const std::string reference -


i'm playing around references in c++, , noted bit of odd behavior unable explain. understanding if have non-const variable , const reference same variable , modify non-const variable, reference should reflect 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 following output me:

x = 10 y = 10 

however, if change type std::string, const reference doesn't seem reflect modified 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 following me:

x = xyz y = abc 

is normal expected behavior when attempting std::string? (i'm using gcc 4.6.0; don't have other compiler available @ moment, don't know if happening specific version or not.)

works totally fine , expected me with gcc 4.3.4 , 4.5.1 aswell offline msvc 10. not showing code execute seems, or there bug in 4.6.0 don't believe. sure you're using reference , not const std::string in real code?


Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -