I have class:
class MyFunctionRunner{
public:
static callFunc(){
func_(); //Calls the function that's set
}
static setFunc(std::function<void()> func){
func_ = func; //Sets a function
}
private:
static std::function<void()> func_;
}
And a function:
void MyFunc(const std::string& a,const std::string& b){
MyClass myObj{};
MyFunctionRunner::setFunc(
[&myObj](){
myObj.setA(a);
myObj.setB(b);
}); //Passes in a lambda function as a parameter that captures `myObj` as reference.
}
In my main, I have:
myFunc("a","b");
MyFunctionRunner::callFunc();
This compiles fine but hits a runtime error of "stack-use-after-return" because I guess it's by the time I run the function, myObj is out of scope? What can I do to extend the scope/lifetime of myObj reference ? Assume that MyClass's implementation (out of my control) has prevented copy constructor ("copy constructor is implicitly deleted...")