P
PKH
This is a part of an autopointer template i'm using. I originally had a
Get() member that returned the pointer, but thought it would nice to be able
to use the -> operator.
The question is why does the following line work, and call
cAP.m_pcPointer->Testing() ?
cAP->Testing(); // ???
cAP-> returns a CTest*, so it looks to me like the line reads
cAP.m_pcPointer <whitespace> Testing(), but it actually does
cAP.m_pcPointer->Testing(). I thought maybe it would be called with a bad
this-pointer, but it's the this-pointer is correct inside Testing(). What am
I not seeing here ?
template <class T> class TAutoPointer
{
private:
T
m_pcPointer;
public:
TAutoPointer(T t) : m_pcPointer(t) {}
~TAutoPointer(){if (m_pcPointer) delete m_pcPointer;}
T& operator -> ()
{
return m_pcPointer;
}
};
class CTest
{
int
m_nTest;
public:
CTest() : m_nTest(123) {}
void Testing()
{
int
i = 0;
}
};
typedef TAutoPointer < CTest* > CTestAutoPtr;
int main(int argc, char* argv[])
{
CTestAutoPtr
cAP(new CTest);
cAP->Testing();
return 0;
}
Get() member that returned the pointer, but thought it would nice to be able
to use the -> operator.
The question is why does the following line work, and call
cAP.m_pcPointer->Testing() ?
cAP->Testing(); // ???
cAP-> returns a CTest*, so it looks to me like the line reads
cAP.m_pcPointer <whitespace> Testing(), but it actually does
cAP.m_pcPointer->Testing(). I thought maybe it would be called with a bad
this-pointer, but it's the this-pointer is correct inside Testing(). What am
I not seeing here ?
template <class T> class TAutoPointer
{
private:
T
m_pcPointer;
public:
TAutoPointer(T t) : m_pcPointer(t) {}
~TAutoPointer(){if (m_pcPointer) delete m_pcPointer;}
T& operator -> ()
{
return m_pcPointer;
}
};
class CTest
{
int
m_nTest;
public:
CTest() : m_nTest(123) {}
void Testing()
{
int
i = 0;
}
};
typedef TAutoPointer < CTest* > CTestAutoPtr;
int main(int argc, char* argv[])
{
CTestAutoPtr
cAP(new CTest);
cAP->Testing();
return 0;
}