J
JFR
i found this code sample on the net that i wanted to use:
class CUniqueObject
{
private:
CUniqueObject( void ) : m_iValue(0) { }
~CUniqueObject( void ) { }
public:
void SetValue( int iValue ) { m_iValue = iValue; }
int GetValue( void ) { return m_iValue; }
static CUniqueObject *GetInstance( void )
{
if( m_pSingleton == 0 )
{
std::cout << "creating singleton." << std::endl;
m_pSingleton = new CUniqueObject;
}
else
{
std::cout << "singleton already created!" << std::endl;
}
return m_pSingleton;
}
static void Kill( void )
{
if( m_pSingleton != 0 )
{
delete m_pSingleton;
m_pSingleton = 0;
}
}
private:
int m_iValue;
static CUniqueObject *m_pSingleton;
};
CUniqueObject *CUniqueObject::m_pSingleton = 0;
int main( int argc, char *argv[] )
{
CUniqueObject *pObj1, *pObj2;
pObj1 = CUniqueObject::GetInstance();
pObj2 = CUniqueObject::GetInstance();
pObj1->SetValue( 11 );
std::cout << "pObj1::m_iValue = " << pObj1->GetValue() << std::endl;
std::cout << "pObj2::m_iValue = " << pObj2->GetValue() << std::endl;
pObj1->Kill();
return 0;
}
when i run the above code and debug it, the pObj1 shows that it
contains 2 things, a m_iValue and a m_pSingleton. However, the
m_pSingleton contains a CUniqueObject* which in turns also contains a
m_iValue and a m_pSingleton, and this goes on and on. I can expand the
object trees in the debugger infinitely.
At first i thought there was a problem with the code, but i think it's
just the debugger's way of showing objects.
Is this behavior "normal"?
class CUniqueObject
{
private:
CUniqueObject( void ) : m_iValue(0) { }
~CUniqueObject( void ) { }
public:
void SetValue( int iValue ) { m_iValue = iValue; }
int GetValue( void ) { return m_iValue; }
static CUniqueObject *GetInstance( void )
{
if( m_pSingleton == 0 )
{
std::cout << "creating singleton." << std::endl;
m_pSingleton = new CUniqueObject;
}
else
{
std::cout << "singleton already created!" << std::endl;
}
return m_pSingleton;
}
static void Kill( void )
{
if( m_pSingleton != 0 )
{
delete m_pSingleton;
m_pSingleton = 0;
}
}
private:
int m_iValue;
static CUniqueObject *m_pSingleton;
};
CUniqueObject *CUniqueObject::m_pSingleton = 0;
int main( int argc, char *argv[] )
{
CUniqueObject *pObj1, *pObj2;
pObj1 = CUniqueObject::GetInstance();
pObj2 = CUniqueObject::GetInstance();
pObj1->SetValue( 11 );
std::cout << "pObj1::m_iValue = " << pObj1->GetValue() << std::endl;
std::cout << "pObj2::m_iValue = " << pObj2->GetValue() << std::endl;
pObj1->Kill();
return 0;
}
when i run the above code and debug it, the pObj1 shows that it
contains 2 things, a m_iValue and a m_pSingleton. However, the
m_pSingleton contains a CUniqueObject* which in turns also contains a
m_iValue and a m_pSingleton, and this goes on and on. I can expand the
object trees in the debugger infinitely.
At first i thought there was a problem with the code, but i think it's
just the debugger's way of showing objects.
Is this behavior "normal"?