Returning an address to a local variable vs returning a pointer to a local variable
I have this in my testing.cpp: class Supp{ public: virtual Supp* add(Supp& val) = 0; }; class SubA : public Supp{ public: int val; SubA(int a){ val = a; } int getVal(){ return val; } Supp* add(Supp& value){ SubA& a = dynamic_cast<SubA&>(value); int tempVal = a.getVal(); int sum = val + tempVal; SubA b =SubA(sum); return &b; } }; and the lines SubA b = SubA(sum); return &b; gives and error because itreturns the address to a local variable which is very bad to do, so i changed it to SubA* b =new SubA(sum); return b; and it works fine with no errors, But is this not basically the same thing? why