shared children disappear

This commit is contained in:
Сергей Маринкевич
2025-07-21 19:28:17 +07:00
parent b2be9b51ca
commit 44480a7128
5 changed files with 43 additions and 222 deletions
+30 -14
View File
@@ -1,4 +1,4 @@
#include "nodes/SimpleNode.h"
#include "nodes/SimpleNode.h" // Путь изменен
#include <iostream>
void printTree(const NodePtr& startNode, int indent = 0) {
@@ -12,24 +12,40 @@ void printTree(const NodePtr& startNode, int indent = 0) {
}
int main() {
auto root = SimpleNode::create("Root");
auto child1 = SimpleNode::create("Child1");
auto child2 = SimpleNode::create("Child2");
std::cout << "Entering main scope...\n\n";
root->linkChild(child1);
root->linkChild(child2);
// Начало новой области видимости
{
auto root = SimpleNode::create("Root");
auto child2 = SimpleNode::create("Child2");
auto subchild = SimpleNode::create("SubChild");
child1->linkChild(subchild);
root->linkChild(child2);
std::cout << "Initial tree:\n";
printTree(root);
{
auto child1 = SimpleNode::create("Child1");
root->linkChild(child1);
auto subchild = SimpleNode::create("SubChild");
child1->linkChild(subchild);
auto child3 = SimpleNode::create("Child3");
root->linkChild(child3);
auto subchild2 = SimpleNode::create("SubChild2");
child3->linkChild(subchild2);
std::cout << "\nUnlinking Child1...\n";
child1->unlinkParent();
std::cout << "Initial tree:\n";
printTree(root);
std::cout << "\nFinal tree:\n";
printTree(root);
std::cout << "\nUnlinking Child1...\n";
child1->unlinkParent();
std::cout << "\nTree after unlink:\n";
printTree(root);
}
std::cout << "\nTree after scope out:\n";
printTree(root);
} // <--- КОНЕЦ ОБЛАСТИ ВИДИМОСТИ
std::cout << "\nExited main scope. All smart pointers destroyed.\n";
return 0;
}