From: Matt L. <ml...@le...> - 2004-09-27 15:01:42
|
> I just think of one unfortunate side effect of using dynamic_cast<*> > instead of cast_to_*(): > > While one can write > void f(vsol_curve_2d_sptr c) { > vsol_line_2d_sptr l = c->cast_to_line(); > } > one cannot write > void f(vsol_curve_2d_sptr c) { > vsol_line_2d_sptr l = dynamic_cast<vsol_line_2d*>(c); > } > and it is necessary to write > void f(vsol_curve_2d_sptr c) { > vsol_line_2d_sptr l = dynamic_cast<vsol_line_2d*>(c.ptr()); > } > which is not so nice since using .ptr() on smart pointers actually > circumvents the smartness of the pointer, which could be dangerous > (not in this context, but once "ptr()" starts to be used often enough, > people will start using it in other situatons as well :-{ > > > -- Peter. I agree that this is a problem, but it's a problem related to the smart pointer and goes beyond the use of dynamic_cast. The same problem occurs when upcasting smart pointers. For example: one cannot write void f(vsol_line_2d_sptr l) { vsol_curve_2d_sptr c = l; } and it is necessary to write void f(vsol_line_2d_sptr l) { vsol_curve_2d_sptr c = l.ptr(); } This problem is somewhat handled in vgui for tableau. Tableau classes create smart pointers that are a subclass of the smart pointers of their parent class. The result is a parallel inheritance hierarchy of smart pointers that mimics that of the classes that they point to. Unfortunately, this requires the use of special vgui_tableau_new classes to construct these smart pointers. This approach is not widely used throughout the rest of VXL, and admittedly wouldn't help with down casting. The point is we already have this unfortunate need for .ptr() even without the dynamic_cast :( -Matt Leotta |