NULL pointer and null references

Discussion in 'C++' started by Vincent RICHOMME, Jan 15, 2006.

  1. 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 ;
    }
    Vincent RICHOMME, Jan 15, 2006
    #1
    1. Advertising

  2. Vincent RICHOMME wrote:
    > 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
    Thomas Tutone, Jan 15, 2006
    #2
    1. Advertising

  3. On Sun, 15 Jan 2006 22:37:11 +0100, Vincent RICHOMME
    <> wrote:

    >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.

    --
    Bob Hairgrove
    Bob Hairgrove, Jan 15, 2006
    #3
  4. Vincent RICHOMME

    TB Guest

    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.

    --
    TB @ SWEDEN
    TB, Jan 15, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexander Stippler

    initializing references / null pointer

    Alexander Stippler, Oct 18, 2003, in forum: C++
    Replies:
    1
    Views:
    354
    Rob Williscroft
    Oct 18, 2003
  2. Roger Leigh
    Replies:
    8
    Views:
    407
    Karl Heinz Buchegger
    Nov 17, 2003
  3. aneuryzma
    Replies:
    3
    Views:
    680
    Jim Langston
    Jun 16, 2008
  4. Replies:
    4
    Views:
    1,218
    Fred Zwarts
    Jul 2, 2009
  5. Christopher
    Replies:
    4
    Views:
    418
    Ruben Safir
    Jul 9, 2011
Loading...

Share This Page