Back
Close

std::shared_ptr cyclic references

Anonymous
1,100 views

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
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants