By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

null object in c++

P: n/a
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

Mar 8 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
Mar 8 '07 #2

P: n/a
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
Mar 8 '07 #3

P: n/a
"toton" <ab*******@gmail.comwrote in message
news:11**********************@v33g2000cwv.googlegr oups.com...
: 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::optional, 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

Mar 8 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.