null object in c++

Discussion in 'C++' started by toton, Mar 8, 2007.

  1. toton

    toton Guest

    Hi,
    In some cases when my function returns, I need to return a object of
    null state. This is when I return object by value.
    (Just like for by pointer, I can return a null pointer ).
    It has to be equivalent to null (or uninitialized state) , just
    like null iterator (or end iterator) .
    has someone worked with the concept of a generic null object , i.e
    any object with a null object trait can be treated as null object or
    like that, with their value comparison semantic etc, so that I dont
    need to define a null state for each class I create explicitly.

    Thanks
    abir
     
    toton, Mar 8, 2007
    #1
    1. Advertising

  2. toton

    benben Guest

    toton wrote:
    > Hi,
    > In some cases when my function returns, I need to return a object of
    > null state. This is when I return object by value.
    > (Just like for by pointer, I can return a null pointer ).
    > It has to be equivalent to null (or uninitialized state) , just
    > like null iterator (or end iterator) .
    > has someone worked with the concept of a generic null object , i.e
    > any object with a null object trait can be treated as null object or
    > like that, with their value comparison semantic etc, so that I dont
    > need to define a null state for each class I create explicitly.
    >
    > Thanks
    > abir
    >


    Why don't you just throw an exception when you can't return a fully
    constructed object?

    Ben
     
    benben, Mar 8, 2007
    #2
    1. Advertising

  3. toton

    benben Guest

    benben wrote:
    > toton wrote:
    >> Hi,
    >> In some cases when my function returns, I need to return a object of
    >> null state. This is when I return object by value.
    >> (Just like for by pointer, I can return a null pointer ).
    >> It has to be equivalent to null (or uninitialized state) , just
    >> like null iterator (or end iterator) .
    >> has someone worked with the concept of a generic null object , i.e
    >> any object with a null object trait can be treated as null object or
    >> like that, with their value comparison semantic etc, so that I dont
    >> need to define a null state for each class I create explicitly.
    >>
    >> Thanks
    >> abir
    >>

    >
    > Why don't you just throw an exception when you can't return a fully
    > constructed object?
    >
    > Ben


    Ok fair enough, I see your point. If you don't want to throw an
    exception, then you need to

    1) somehow design the class so they can represent a null state;
    2) have a null object to compare to. Typically, class Null{}; Null null;
    will do; and
    3) define operator == and !=

    Ben
     
    benben, Mar 8, 2007
    #3
  4. "toton" <> wrote in message
    news:...
    : Hi,
    : In some cases when my function returns, I need to return a object of
    : null state. This is when I return object by value.
    : (Just like for by pointer, I can return a null pointer ).
    : It has to be equivalent to null (or uninitialized state) , just
    : like null iterator (or end iterator) .
    : has someone worked with the concept of a generic null object , i.e
    : any object with a null object trait can be treated as null object or
    : like that, with their value comparison semantic etc, so that I dont
    : need to define a null state for each class I create explicitly.

    You may want to consider boost::eek:ptional, or at least be interested in
    the discussion at http://www.boost.org/libs/optional/doc/optional.html

    Regards,
    Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Brainbench MVP for C++ <> http://www.brainbench.com
     
    Ivan Vecerina, Mar 8, 2007
    #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. Replies:
    16
    Views:
    7,350
    Mike Schilling
    Oct 12, 2005
  2. Replies:
    5
    Views:
    26,735
    Mike Schilling
    Mar 29, 2006
  3. Cirene
    Replies:
    1
    Views:
    819
    Alexey Smirnov
    Jun 9, 2008
  4. putty
    Replies:
    1
    Views:
    260
    putty
    Apr 5, 2005
  5. Tim Platt

    XMLHTTP - null is null or not an object

    Tim Platt, Jun 4, 2007, in forum: Javascript
    Replies:
    0
    Views:
    181
    Tim Platt
    Jun 4, 2007
Loading...

Share This Page