TheBlackSword TheBlackSword - 1 year ago 92
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?

Answer Source

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,

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download