Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
Cyclic reference with std::shared_ptr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <type_traits>
#include <memory>
struct Node { // Binary tree
Node() { std::cout << "c'tor" << std::endl; }
~Node() { std::cout << "d'tor" << std::endl; }
std::weak_ptr<Node> parent;
std::shared_ptr<Node> left;
std::shared_ptr<Node> right;
};
int main() {
auto root = std::make_shared<Node>();
root->left = std::make_shared<Node>();
root->left->parent = root;
}
Enter to Rename, Shift+Enter to Preview
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content