Cast auto_ptr<Base> to auto_ptr<Derived>

你说的曾经没有我的故事 提交于 2019-12-25 00:46:24

问题


Please help me to understand the following issue.

Look at the code example below:

#include <iostream>

class Shape {
public:
  virtual wchar_t *GetName() { return L"Shape"; }
};
class Circle: public Shape {
public:
  wchar_t *GetName() { return L"Circle"; }
  double GetRadius() { return 100.; }
};

int wmain() {
  using namespace std;

  auto_ptr<Shape> aS;
  auto_ptr<Circle> aC(new Circle);

  aS = aC;
  wcout << aS->GetName() << L'\t' << static_cast<auto_ptr<Circle>>(aS)->GetRadius() << endl;

  return 0;
}

Why I am not allowed to do this:

static_cast<auto_ptr<Circle>>(aS)->GetRadius()

Compiler (MSVCPP 11):

1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory(911): error C2440: 'initializing' : cannot convert from 'Shape *' to 'Circle *'
1>          Cast from base to derived requires dynamic_cast or static_cast

回答1:


auto_ptr doesn't behave the same way as a pointer in this respect. There are special rules in the language to allow Shape* to be static_cast to Circle* when Circle derives from Shape. The downcast is not entirely type-safe, since it relies on the user to provide a pointer value that actually does point to the Shape base class sub-object of a Circle, but the standard allows it for convenience. auto_ptr is "just" a library class, and has no equivalent conversion.

Even if you could do it, it would often go wrong. When you copy an auto_ptr, the original loses ownership of the resource. Your static_cast would copy the auto_ptr to a temporary, and so aS would be reset and the resource would be destroyed when the temporary is (at the end of the expression). In your example that's fine, since it's going to be destroyed at return anyway, but generally speaking you don't want to copy auto_ptr except at a function call parameter or return value, to indicate transfer of ownership from caller to callee, or vice versa.

What you can do instead is static_cast<Circle*>(aS.get())->GetRadius(), or better yet restructure your code to avoid the need for a downcast. If you know that your object is a Circle, keep it in an auto_ptr<Circle>[*]. If you keep it in an auto_ptr<Shape>, then don't rely on it being 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 doesn't provide them, there's Boost.




回答2:


You certainly don't wanna do that cast, since std::auto_ptr<T> takes ownership of the internal pointer when initialized with another instance of the class.

aS will therefore loose the pointer and your new Circle object will be destroyed at the end of the std::cout statement, since the object-pointer is now owned by a temporary.

Instead you are probably looking for something like the below:

cout << ... << static_cast<Circle*>(aS.get ())->GetRadius() << endl;

You may also cast it to a reference, as below:

cout << ... << static_cast<Circle&> (*aS).GetRadius () << endl;


来源:https://stackoverflow.com/questions/8574022/cast-auto-ptrbase-to-auto-ptrderived

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!