TheBlackSword TheBlackSword - 10 months ago 39
C++ Question

passing a null pointer of an object into a function

If I have

class* object = NULL;
, and I pass
function(class* arg){arg = new class();}
, does
object = new class()

I ask because a pointer is just an address, right? So if I pass a NULL pointer as an argument and assign a new object to it, the address will be changed from zero, and the original pointer will not point to the new object, correct?


Inside the function, arg will point to the new object; but as you passed a copy of the pointer, once the function is over, it will not be accessible anymore, and object will not point to it (but still to NULL).

You need to pass either a reference to object (class*&), or a pointer to object (class**), to enable the function to modify it,