NULL pointer and null references

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 ;
}
 
T

Thomas Tutone

Vincent said:
Hi,

I would like to know how I can return a NULL reference.

You can't. References by definition are an alias for an actual object.
You can, however, return a reference to a sentinel object that you
create.
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) :

[code snipped]
Do I need to do that, I return a boolean to know if reference is valid
or is there another solution

You should design your programs so that all your references are valid -
anything else involves undefined behavior. Either use pointers (or
better yet, some sort of smart pointer), or use a sentinel object.

You might also look at the FAQ:

http://www.parashift.com/c++-faq-lite/references.html#faq-8.6

Best regards,

Tom
 
B

Bob Hairgrove

Hi,

I would like to know how I can return a NULL reference.

You cannot. What Thomas said...
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) :

[snip]

In addition to Thomas' reply, you might consider the possibility of
throwing an exception if it is an error when you cannot return a
reference to a valid object from the function. Otherwise, if it is to
be expected that the object might not exist sometimes, etc., return a
pointer instead.

Throwing an exception is sometimes the best thing to do. Like Thomas
said, though, it is really a design decision that only you can make.
 
T

TB

Vincent RICHOMME sade:
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;
}

Is it common that this occurs or is it an exception (ie it
should never happen)? If it's the latter then throw an exception,
otherwise rethink your design.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top