Boost logo

Boost Users :

From: Fernando Cacciola (fernando_cacciola_at_[hidden])
Date: 2006-08-03 09:31:36


Ovanes Markarian wrote:
> Hello all,
>
> I have one specific question. Which I am uncertain if it can be solved anyway.
>
>
> Imagine the following class structure:
>
>
> struct A
> {
> some_field* x;
> };
>
>
> struct B
> {
> optional<A> a_;
> };
>
>
> struct C
> {
> optional<B> b_;
> };
>
>
> Now I have an instance of struct C and would like to know if x is a valid pointer.
>
> this code is really ugly:
>
> if(c.b_)
> if(c.b_.a_)
> if(c.b_.a_.x)
> //do smth ...
>
>
If you need to access x form c (which is what are effectively doing
here) then you should provide an accessor to x in c itself (and in B by
transition).

struct B
{
   some_field* x() { return a_ ? a_->x : 0 ; }

   optional<A> a_;
};

struct C
{
   some_field* x() { return a()->x() ; }

   // this could be private
   optional<A> a() { return b_ ? b_->a_ : none ; }

   optional<B> b_;
};

if ( c.x() )
   ....

HTH

Fernando Cacciola
fcacciola.50webs.com


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net