I've been searching around for the answer to this and haven't found anything that was similar to my problem.
I have a class, let's call it Foo
, that takes a const char*
pointer in it's constructor. In my code, I need to create a new Foo
with a std::string
's .data()
as the parameter. The problem is, once the string falls out of scope, the value (or pointer, this is where I'm getting confused..) passed to Foo becomes invalid.
So now that the string is invalid, the Foo's const char*
is invalid.
How can I pass the value of the string's data into Foo
so that it won't become invalid once the string falls out of scope?
You should probably be copying the string inside
Foo
to store it if you need it to last past the constructor's calling scope.