Code:
#include <iostream>
class GlobalClass
{
static GlobalClass *s_instance;
public:
static GlobalClass *instance()
{
if (!s_instance)
s_instance = new GlobalClass;
return s_instance;
}
void SetPointer(void *ptr)
{
this->PointerSetter(ptr);
std::cout << "ptr:" << ptr << std::endl;
}
void PointerSetter(void *ptr)
{
int Value = 1;
ptr = &Value;
std::cout << "ptr:" << ptr << std::endl;
}
};
GlobalClass *GlobalClass::s_instance = 0;
int main()
{
void *ptr=NULL;
std::cout << "ptr:" << ptr << std::endl;
GlobalClass::instance()->SetPointer(ptr);
std::cout << "ptr:" << ptr << std::endl;
return 0;
}
Output:
ptr:0
ptr:0x6ffddc
ptr:0
ptr:0
I am not able to understand why the pointer that I passed to function SetPointer
in the singleton instance of GlobalClass
which in turn is passed to PointerSetter
is not visible when it comes out of PointerSetter
. I need to access it in my main
function.
How is the pointer is being set to NULL or 0?