Добрый день!

Познаю c++

#include <iostream>
#include <memory>

class Foo {
public:
Foo(const bool needСhildren);
~Foo();
bool isParent;
std::unique_ptr<Foo> children;
};

Foo::Foo(const bool needChildren) {
std::cout << "Construct new Foo" << std::endl;
if (needChildren) {
this->isParent = true;
this->children = std::make_unique<Foo>(false);
} else {
this->isParent = false;
}

}

Foo::~Foo(void) {
std::cout << "Destruct";
if (isParent) {
std::cout << " parent ";
} else {
std::cout << " children ";
}
std::cout << " Foo" << std::endl;
}

void foo() {
std::cout << "call foo()" << std::endl;
std::make_unique<Foo>(true);
std::cout << "end foo()" << std::endl;
}

int main() {
foo();
return 0;
}

В консоли

call foo()
Construct new Foo
Construct new Foo
Destruct parent Foo
Destruct children Foo
end foo()

Объясните, ткните в стандарт или хоть какое нибудь описание - как вызывается деструктор поля children если в деструкторе явно это не определено? Как поле unique_ptr понимает, что «пора» самоуничтожаться?








 , ,






URL записи