null object in c++

T

toton

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
 
B

benben

toton said:
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
 
B

benben

benben said:
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
 
I

Ivan Vecerina

: 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
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top