std :: unique_ptr не следует копировать в другой std :: unique_ptr, если вы не используете std :: move, правильно?
Но я сталкиваюсь с чем-то подобным.
#include <iostream>
#include <memory>
using namespace std;
class ClassA{
public:
ClassA() {
cout << "created" << endl;
}
~ClassA() {
cout << "destroyed" << endl;
}
void print() {
cout << "ok" << endl;
}
};
void test() {
unique_ptr<ClassA> b(new ClassA());
b->print();
// unique_ptr<ClassA> d(b); this won't compile since you can't copy unique_ptr
ClassA& aa = *b; // ClassA aa = *b; works too.
unique_ptr<ClassA> d(&aa); // but this can, why is that
d->print();
}
int main(int argc, char *argv[])
{
test();
return 0;
}
Программа работает нормально, вывод:
created
ok
ok
destroyed
destroyed
ClassA был уничтожен дважды, как это происходит? Должен ли он взломать sh из-за нечего удалять?