Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > null object in c++

Reply
Thread Tools

null object in c++

 
 
toton
Guest
Posts: n/a
 
      03-08-2007
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

 
Reply With Quote
 
 
 
 
benben
Guest
Posts: n/a
 
      03-08-2007
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
 
Reply With Quote
 
 
 
 
benben
Guest
Posts: n/a
 
      03-08-2007
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
 
Reply With Quote
 
Ivan Vecerina
Guest
Posts: n/a
 
      03-08-2007
"toton" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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: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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASPX page jscript rt error: null is null or not an object Cirene ASP .Net 1 06-09-2008 07:59 PM
XMLHTTP - null is null or not an object Tim Platt Javascript 0 06-04-2007 08:42 AM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM
difference between null object and null string gokul.b@gmail.com Java 16 10-12-2005 06:43 PM
IE SP2 solution for "null is null or not an object" and "broken" insertCell/insertRow putty Javascript 1 04-05-2005 07:11 PM



Advertisments