std::shared_ptr cyclic references

Anonymous
1,592 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Cyclic reference with std::shared_ptr

#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;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content