Boost logo

Boost :

From: Pavol Droba (droba_at_[hidden])
Date: 2004-10-08 16:30:49


On Fri, Oct 08, 2004 at 10:49:20PM +0200, Thorsten Ottosen wrote:
> "Pavol Droba" <droba_at_[hidden]> wrote in message
> news:20041008202443.GA22250_at_lenin.felcer.sk...
>

> | class Null : public Base
> | {
> | virtual int Op1() { return special_value1; }
> | ..
> | virtual int Op100() { return special_value100; }
> | };
>
> I would return 0 here, in all cases.
>

Please try to look little bit futher. This is an illustrative example,
so int can be for example a reference to a memory or an new object.
In any case, there is not always a nice 0 like here.

So no it would not return 0. 0 is a valid value here.
null object is not supposed to do this.

 
> | // Now some operations, that use the object of Base.
> | // Assume we have 0 pointers
> |
> | int foo1(Base* pObj)
> | {
> | // Simple case -> check if oObj is not null
> | if(pObj==0)
> | return special_value;
> | else
> | return pObj->Op1() + pObj()->Op2() + ...... + pObj()->Op100();
> | }
>
> int foo1( Base& pObj )
> {
> return pObj->op1() + ... + pObj->op100();
> }
>
> | This example might look realy stupid, but it illustrates well the problem
> I'm refering to.
> | You cannot assume, that null object will work in the same way as an ordinary
> one. Otherwise
> | you only pospone the checking one level further -> to its operations and the
> objects, that
> | uses it.
>
> Yeah, that is really the issue here: are we just forcing users to defer the
> comparinson for "nullness"
> yet another level. Am eager to be proven wrong, but It's going to take more
> convincing examples.
>

What more convincing do you need? You have just said, that you force users to
pospone the null check to another level. Considering the next level more
complicated (complexity usualy grows exponentialy as you go to another level,
because the number of affected entities usualy multiplies on every level)
I see no benefits of doing this.

> I would like to see real examples and then see if I can recode them so we can
> compare. But it has
> to be real examples (you must have plenty from your work? Just take one where
> you use shared_ptr).
> But seriously, I don't recall the need for 0's for a long time.

Not plenty, but indeed I have the cases. IIRC I have already described
one example.

I'm using a container of smart pointer of a fixed size. Each place is designated
as a slot and althouch all members of container have common predecessor and functionality,
each slot has a special meaning.
Generaly, the container is used from more then entity. For one it is just a bunch
of objects that are used in an order, for another entity, each slot have a different meaning.
There are even assertions for a specific types, that have be present on a specific slot.

Slot can be empty of cause. In that case, first entity simply skips it, second entity
is also aware of it an acts accordingly.

If I would go your way, I will need a special class for every slot, because from the
POV of the second entity, they are all different.

Do you need a code to see this?

Regards,

Pavol


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk