V
Vincent RICHOMME
Hi,
I would like to know how I can return a NULL reference.
usually when I need to return an object and to know if the object
is valid I use a pointer (if the object is nto valid I return NULL) :
MyObj* MyClass::GetObj(const wxString& strObjName )
{
ReaderIt it = m_scObjs.find( strObjName );
// No Object with this name
if ( it == m_scObjs.end() ){
return NULL;
}
// Get pointer in Reader obj and set new name
MyObj* pMyobj = (MyObj*) it->second;
return pMyobj ;
}
Now Let's say I want to return a reference
MyObj& MyClass::GetObj(const wxString& strObjName )
{
ReaderIt it = m_scObjs.find( strObjName );
// No Object with this name
if ( it == m_scObjs.end() ){
return ??????????NULL;
}
// Get pointer in Reader obj and set new name
MyObj* pMyobj = (MyObj*) it->second;
return *pMyobj ;
}
Do I need to do that, I return a boolean to know if reference is valid
or is there another solution
bool MyClass::GetObj(const wxString& strObjName, MyObj& refMyobj )
{
ReaderIt it = m_scObjs.find( strObjName );
// No Object with this name
if ( it == m_scObjs.end() ){
return false;
}
// Get pointer in Reader obj and set new name
MyObj* pMyobj = (MyObj*) it->second;
refMyobj = *pMyobj;
return true ;
}
I would like to know how I can return a NULL reference.
usually when I need to return an object and to know if the object
is valid I use a pointer (if the object is nto valid I return NULL) :
MyObj* MyClass::GetObj(const wxString& strObjName )
{
ReaderIt it = m_scObjs.find( strObjName );
// No Object with this name
if ( it == m_scObjs.end() ){
return NULL;
}
// Get pointer in Reader obj and set new name
MyObj* pMyobj = (MyObj*) it->second;
return pMyobj ;
}
Now Let's say I want to return a reference
MyObj& MyClass::GetObj(const wxString& strObjName )
{
ReaderIt it = m_scObjs.find( strObjName );
// No Object with this name
if ( it == m_scObjs.end() ){
return ??????????NULL;
}
// Get pointer in Reader obj and set new name
MyObj* pMyobj = (MyObj*) it->second;
return *pMyobj ;
}
Do I need to do that, I return a boolean to know if reference is valid
or is there another solution
bool MyClass::GetObj(const wxString& strObjName, MyObj& refMyobj )
{
ReaderIt it = m_scObjs.find( strObjName );
// No Object with this name
if ( it == m_scObjs.end() ){
return false;
}
// Get pointer in Reader obj and set new name
MyObj* pMyobj = (MyObj*) it->second;
refMyobj = *pMyobj;
return true ;
}