#include <iostream> using namespace std; class B { public: void invoke(){ cout << "invoke"; } }; class A{ public: B* test(){ B b; auto *bb = &b; return bb; } }; int main() { A a; B *b = a.test(); b->invoke(); b->invoke(); return 0; } A few questions about this code:
- will it be correct?
- why the compiler does not swear at return bb;
- why this code works, because auto * bb is a local variable (stack) and must die with everything together when the method ends (return)