Why does C ++ destroy a class Parentbefore a class Child? Wouldn’t it be more logical for an object when it goes out of scope to destroy first shared_ptrand then destroy itself? In my workflow, this causes a problem because my class Parentcontrols the interface that the classes use Child.
#include <iostream>
#include <memory>
class Child;
class Parent
{
public:
Parent() :
child(std::make_shared<Child>())
{
std::cout << "Constructing parent" << std::endl;
}
~Parent() { std::cout << "Destructing parent" << std::endl; }
private:
std::shared_ptr<Child> child;
};
class Child
{
public:
Child()
{
std::cout << "Constructing child" << std::endl;
}
~Child() { std::cout << "Destructing child" << std::endl; }
};
int main()
{
Parent parent;
return 0;
}
, , . std::shared_ptr, , . - CUDA, GPU. , . , , GPU, , , . , , , , .