I wrote sample code that should cause access violation, but it does not.
I think the exception should occur in GetSession1()
, GetSession2()
function for return *m_pObj
, but it does not. Why is that?
Header file
class CSession
{
public:
CSession() {};
~CSession() {};
CSession(const CSession& rhs) {};
private:
long m_lUSN;
};
class CTest
{
public:
CSession* m_pObj;
CSession& GetSesstion1() { m_pObj = NULL; return *m_pObj; }
CSession GetSesstion2(); { m_pObj = NULL; return *m_pObj; }
};
Cpp file
int _tmain(int argc, _TCHAR* argv[])
{
CTest test;
CSession Session2 = test.GetSesstion1();
CSession Session3 = test.GetSesstion2();
return 0;
};