1

If I have some object pointing to NULL and i want to assign it using new in the scope of a function, why is the lifetime of this object limited to the scope?

SomeObject *o = nullptr;

void f(SomeObject *o)
{
    o = new SomeObject; //Not null
}

o->doWhatever(); //Null again

And: how can i assign someObject else then (without returning)?

tobilocker
  • 891
  • 8
  • 27
  • 4
    Ages-old question -- you passed `o` by value into the function, so the original `o` is unaffected. – Quentin Feb 08 '17 at 13:13

1 Answers1

0

The input argument that you are passing to function f has the same name as the global variable.

So the assignment inside function f applies for the local variable instead of the global variable.

barak manos
  • 29,648
  • 10
  • 62
  • 114