i have a function that does this:
static MyClass* MyFunction(myparams)
{
return new MyClass(myparams)
}
and i would be able to call this function inside another one that has the following signature:
void MyFunction2(std::auto_ptr<MyClass> myparam)
but when I try to do it i have a compiler error:
Impossible to convert the first param from MyClass * to std::auto_ptr<_Ty>
why? Thank you for any help
EDIT 1 As asked the myparams types are normal but there is also a T param because the function is inside a template class
std::auto_ptr<>
has an explicit constructor, like any other smart pointer. That means that there is no implicit conversion fromT*
tostd::auto_ptr<T>
to prevent from accidentally deleting an object. Hence you need to convert your raw pointed tostd::auto_ptr<>
explicitly:It is also a good idea to make your factory functions return a smart pointer instead of a raw pointer, it makes it clear to the reader that the ownership of an object is being transferred to the caller: