J
John Harrison
chandra sekhar said:Hi All,
I am trying to allocate memory for member variable in a static
method belongs to same class.. the allocation is o.k and when try to
access the member variable I find that it is a null pointer.
Here is the code ...
#include <iostream.h>
class A
{
public:
A():i(0){}
~A(){ if(i) { delete i; i = 0;}}
static void create(A** obj)
{
*obj = new A();
int* ii = 0;
(*obj)->get_i(ii);
ii = new int;
}
void get_i(int*& g_out)
{
g_out = i;
}
private:
int* i;
};
int main()
{
A* ob = 0;
A::create(&ob);
int* i = 0;
ob->get_i(i);
try
{
cout << " value " << *i << endl;
}catch(...)
{
// thows the null pointer exception
}
if( ob ) { delete ob; ob = 0;}
return 0;
}
Any suggestions in this regard is welcome.
Thanks
Chandra-
Nowhere in the code above do you assign to the member variable i, so it
stays NULL. Where did you think you were changing member variable i?
john