fork download
  1. #include <vector>
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5. int main()
  6. {
  7. auto str = std::string("XYZ"); // mutable string
  8. const auto& cstr(str); // const ref to it
  9.  
  10. vector<string> v;
  11. v.push_back(cstr);
  12.  
  13. cout << v.front() << endl; // XYZ is printed as expected
  14.  
  15. *const_cast<char*>(&cstr[0])='*'; // this will modify the first element in the VECTOR (is this expected?)
  16. str[1]='#'; //
  17.  
  18. cout << str << endl; // prints *#Z as expected
  19. cout << cstr << endl; // prints *#Z as expected
  20. cout << v.front() << endl; // Why *YZ is printed, not XYZ and not *#Z ?
  21.  
  22. return 0;
  23. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
XYZ
*#Z
*#Z
XYZ