我看到了下面的代码,
#include <new>
#include <memory>
using namespace std;
class Fred; // Forward declaration
typedef auto_ptr<Fred> FredPtr;
class Fred {
public:
static FredPtr create(int i)
{
return new Fred(i); // Is there an implicit casting here? If not, how can we return
// a Fred* with return value as FredPtr?
}
private:
Fred(int i=10) : i_(i) { }
Fred(const Fred& x) : i_(x.i_) { }
int i_;
};
请参阅函数创建中列出的问题。
谢谢
// 根据评论更新
是的,代码无法传递 VC8.0 错误 C2664: 'std::auto_ptr<_Ty>::auto_ptr(std::auto_ptr<_Ty> &) throw()' : cannot convert parameter 1 from 'Fred *' to ' std::auto_ptr<_Ty> &'
代码复制自 C++ FAQ 12.15。
但是,在进行以下更改后,
replace
return new Fred(i);
with
return auto_ptr<Fred>(new Fred(i));
这段代码可以通过VC8.0编译。但我不确定这是否是一个正确的修复。