I have these code and I found s
will get invalid if it is a C-styled const string:
class has_a_str_ref {
const string& str;
public:
has_a_str_ref(string const& s) : str(s) {}
void show() { std::cout << str << std::endl; }
};
int main() {
const char* s = "abc";
// const char s[] = "abc"; // not work
// string s = "abc"; // works
has_a_str_ref x(s);
std::cout << s << std::endl;
x.show();
std::cout << s << std::endl;
return 0;
}
What happened behind it?
PS: compile with
-fsanitize=address