I have a class
class A {
public:
A(){cout<<"C";}
~A(){cout<<"D";}
};
int main(){
unique_ptr<A> a(new A[5]); // - doesn't work
unique_ptr<A> a(new A[1]); // - doesn't work
unique_ptr<A> a(new A); // - works
}
Why does this happen?
I guess that the thing is about the move constructor (it cannot be created automatically because of the destructor), but why do we need a move constructor here?
And what's the difference between:
unique_ptr<A> a(new A[1]); // - doesn't work
unique_ptr<A> a(new A); // -works
To use unique_ptr
with an array allocation, you need to use a specialization of it:
unique_ptr<A[]> a(new A[5]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With