
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