How come this compiles and works:
class MyObject {
public:
MyObject() {}
};
struct ItemGood {
int Number;
MyObject *Object;
ItemGood(int Number, MyObject *Object) {
this->Number = Number;
this->Object = Object;
}
};
const ItemGood ItemGoodList[] =
{
{ 0, new MyObject() },
{ 1, new MyObject() }
};
And this does not compile at all:
class MyObject {
public:
MyObject() {}
};
struct ItemBad {
int Number;
std::auto_ptr<MyObject> AutoObject;
ItemBad(int Number, MyObject *Object) {
this->Number = Number;
AutoObject = std::auto_ptr<MyObject>(Object);
}
};
const ItemBad ItemBadList[] =
{
{ 0, new MyObject() },
{ 1, new MyObject() }
};
The error the compiler spits out is:
no matching function for call to ‘ItemBad::ItemBad(ItemBad)
I do not understand why something is trying to call that constructor, I don’t understand what is actually happening in this initializer list.
Because
std::auto_ptrdoes not have a proper copy constructor, which means your class does not have a proper copy constructor, which means that it cannot be constructed from a temporary- which is what you’re trying to do.Just junk
auto_ptrand move tounique_ptr.