C++ -- Is there an implicit cast here from Fred* to auto_ptr<Fred>?
Posted
by
q0987
on Stack Overflow
See other posts from Stack Overflow
or by q0987
Published on 2011-01-01T22:16:42Z
Indexed on
2011/01/03
16:54 UTC
Read the original article
Hit count: 372
Hello all,
I saw the following code,
#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_;
};
Please see the question listed in function create.
Thank you
// Updated based on comments
Yes, the code cannot pass the VC8.0 error C2664: 'std::auto_ptr<_Ty>::auto_ptr(std::auto_ptr<_Ty> &) throw()' : cannot convert parameter 1 from 'Fred *' to 'std::auto_ptr<_Ty> &'
The code was copied from the C++ FAQ 12.15.
However, after making the following changes,
replace
return new Fred(i);
with
return auto_ptr<Fred>(new Fred(i));
This code can pass the VC8.0 compiler. But I am not sure whether or not this is a correct fix.
© Stack Overflow or respective owner