Okay I've got to admit first that pointers and references are not my strong point. Sometimes I can imagine and understand how things go, sometimes I don't. This is the simplified version of my problem:
Code:
class TestObj
{
public:
TestObj()
{
mA = 1;
}
~TestObj()
{
}
int getA()
{
return mA;
}
private:
int mA;
};
class TestPointer
{
public:
TestPointer()
{
}
~TestPointer()
{
}
TestObj* get()
{
return mXPointer;
}
void set(const TestObj* pointer)
{
mXPointer = pointer;
}
private:
TestObj* mXPointer;
};
void testFunc(TestObj *& obj)
{
obj = new TestObj();
}
int _tmain(int argc, _TCHAR* argv[])
{
int n;
char str[10];
TestPointer testP;
testFunc(testP.get()); //Error 1 error C2664: 'testFunc' : cannot convert parameter 1 from 'TestObj *' to 'TestObj *&'
return 0;
}
Well, I know that I can solve the problem like below.
Code:
int _tmain(int argc, _TCHAR* argv[])
{
int n;
char str[10];
TestPointer testP;
TestObj *obj;
testFunc(obj);
testP.set(obj);
return 0;
}
But isn't it the same thing actually? Or is it? I'm sorry my logic is kind of messed up right now. So would you mind to elaborate why it was faulty? Thanks.
Oh yeah BTW, the testFunc itself is not actually an instantiation of the TestObj like above. It's just to simplify the actual problem. So no need to bash that function and say that it's not right according to RAII or such.
Thanks in advance.