auto_ptr does not behave the same as a pointer in this regard. There are special rules in the language that allow Shape* be static_cast to Circle* when Circle comes from Shape . Deferred is not completely type safe because it relies on the user to provide a pointer value that actually points to a sub-object of the Shape base class for Circle , but the standard allows this for convenience. auto_ptr is a "simple" library class and does not have an equivalent conversion.
Even if you could do it, this is often a mistake. When you copy auto_ptr , the original loses ownership of the resource. Your static_cast would copy auto_ptr to temporary, and therefore aS would be reset, and the resource would be destroyed when temporary (at the end of the expression). In your example, this is fine, since it will be destroyed in return anyway, but generally speaking, you do not want to copy auto_ptr except for the function call parameter or return value to indicate the transfer of ownership from the caller to the caller, or vice versa.
Instead, you can do static_cast<Circle*>(aS.get())->GetRadius() or better restructure your code to avoid having to drag and drop. If you know your object is Circle , save it in auto_ptr<Circle> [*]. If you save it in auto_ptr<Shape> , then do not rely on it as a Circle .
[*] Or, if your implementation provides them, a better smart pointer such as unique_ptr , scoped_ptr or shared_ptr . Even if your implementation does not provide them, there is Boost.
source share