Why does the following program print garbage instead of hello
? Interestingly, if I replace hello
with hello how are you
, then it prints hello how are you
.
#include <string>
#include <iostream>
class Buffer
{
public:
Buffer(std::string s):
_raw(const_cast<char*>(s.data())),
_buffer(std::move(s))
{
}
void Print()
{
std::cout << _raw;
}
private:
char* _raw;
std::string _buffer;
};
int main()
{
Buffer b("hello");
b.Print();
}
Aucun commentaire:
Enregistrer un commentaire