   Inheritence question
  Take this situation (C++):struct A{   virtual void Foo(const Y &) = 0;};class Y{    virtual void do(const A & a) = 0;};struct B : public A{   B():bar(0){}   void Foo(const Y & z)   { z.do(*this);   }   int bar;};class Z : public Y{    virtual void do(const A & a)    {   ((B)a).bar++;    }};As you can see, A is a subclass of B.  Both have a virtual function, Foo, which takes a reference to a class Z.  But say I knew that B was always going to be passed a reference to class Y, which needs to interact with B's "bar" variable somehow, like above, where it increments it.  When I tried this technique, it always seems that a new temporary instance of B is created during the cast, which would always have a "bar" value of zero.  If I tried doing this, for example:B b;Z z;while (b.bar < 10)    b.Foo(z);The above code would give an infinite loop, because b's "bar" value never actually increases.Is there some easy way to work around this, or am I stuck?  Thanks.   
  Since you don't know whether the A is really a B or just an A, you need to use a dynamic_cast.virtual void do(const A & a){    dynamic_cast<B&>(a).B::bar++;}The B:: won't be strictly necessary (since we're accessing a B), but that's how you specify which one you want.The cast will throw an std::bad_cast exception if you had a plain A and not a real B, meaning thattry{    dynamic_cast<B&>(a).B::bar++;} catch(std::bad_cast&) {}is required. Alternatively, you can work with pointers:B* ptr = dynamic_cast<B*>(&a);if (ptr != 0) ptr->B::bar++;[Edited by - Fruny on May 25, 2006 7:21:58 PM];Quote:((B)a).bar++;Well, you're asking for a temporary copy, so a temporary copy you are getting :)You need to cast the A& into B&. This is possible with the old-style casts, but is not too safe. Better to use the new style:// just to be extra sure - dynamic_cast returns a null pointer if the cast is not possibleassert(dynamic_cast<B*>(&a));// now that we are (hopefully) 100% sure that a is actually a B, we can use static_caststatic_cast<B&>(a).bar++EDIT: If you cannot be sure that the reference passed is always a B, it's better to use Fruny's method.   ;  If you're "sure that a Z is always passed a B", then are you really making use of the polymorphism? I mean, if you have a Y, and you want to pass it an A, you also have to know that the Y is not a Z... any chance we could see a little more of your actual design? I have a vague idea of what you're doing (some kind of Bridge pattern thing) but I'm thinking that this kind of casting usually points to a design problem...   ;Quote:Original post by FrunySince you don't know whether the A is really a B or just an A, you need to use a dynamic_cast.virtual void do(const A & a){    dynamic_cast<B&>(a).B::bar++;}The B:: won't be strictly necessary (since we're accessing a B), but that's how you specify which one you want.The cast will throw an std::bad_cast exception if you had a plain A and not a real B, meaning thattry{    dynamic_cast<B&>(a).B::bar++;} catch(std::bad_cast&) {}is required. Alternatively, you can work with pointers:B* ptr = dynamic_cast<B*>(&a);if (ptr != 0) ptr->B::bar++;I tried dynamic casting, but I didn't think to cast to a reference to an object rather than a new object, which, of course, resulted in a new object being created.  Silly me.  Thanks, you all saved the day![Edited by - Fruny on May 25, 2006 7:58:31 PM]