
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 ... This will be really unpleasent if the structure is more deep then this one. Which is the case in our project. The intention would be to write everything in one if-statement. It is not possible to implement a bool or ! operator in these structures, since these are not the only fields and these operators would not know which field should be checked... Currently I am thinking about boost parameter library, but still uncertain if this problem can be solved using it... Can some of you make a suggestion what is the best suitable way of resolving this issue and if this issue resolvable anyway. With Kind Regards, Ovanes Markarian

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
participants (2)
-
Fernando Cacciola
-
Ovanes Markarian