Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8151097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:16:02+00:00 2026-06-06T15:16:02+00:00

Is this the right way, to use boost::shared_ptr for the nodes left and right

  • 0

Is this the right way, to use boost::shared_ptr for the nodes left and right and boost::weak_ptr for the root. I know shared is an overkill. I will need this for later.

class Node, 2 sons shared ptr

class Node
{
public:
boost::shared_ptr<Node> left;
boost::shared_ptr<Node> rigth;
int nVal;
Node();
Node(int);
~Node();
Node getVal(void);
void setVal(int);

};

Node::Node()
{
cout << "creating node empty" << endl;
nVal = 0;
left.reset();
rigth.reset();

}

Node::~Node()
{
cout << "entering destructor" << endl;
 }

Node::Node(int n)
{
cout << "creating node with value" << endl;
nVal = n;
left.reset();
rigth.reset();
}

Node Node::getVal(void)
{
cout << "returning value" << endl;
return this;
}

void Node::setVal(int n)
{
cout << "setting value" << endl;
nVal = n;
}

class tree, weak ptr

class Tree 
{
public:
boost::weak_ptr<Node> root;
Tree();
~Tree();
void findParent(int n, int &found, Node &parent);
void add(int n);
void post(boost::weak_ptr<Node> q);
void del(int n);

};

Tree::Tree()
{
root = NULL;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T15:16:03+00:00Added an answer on June 6, 2026 at 3:16 pm

    You should not use a weak pointer for the node like this. A weak pointer means that the object may be deleted even if there are still weak pointers available for the object, if no other weak pointers to it exists.

    So in your case you have one weak pointer to the root and no shared pointer. In this situation the root will be deleted, since no shared pointers are keeping it alive and all of the tree will be gone.

    The general rule is, that a shared pointer should be used for ownership relationships. The Tree definitely owns the root of itself. Weak pointer on the other hand should be used for back pointers to direct or indirect parents, to break out of circles. So if you had nodes pointing back to root, this would be a place to use weak pointer.

    The best way to make sure is to first try to use shared pointers, and pay attention not to build any circles in the dependencies. Only when you have to break a circle change a shared pointer to a weak pointer based on the ownership/backpointer difference.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a snippet of code like this: std::list<boost::shared_ptr<Point> > left, right; // ...
I don't know if I'm thinking of this the right way, and perhaps somebody
Right now, I have this code: bool isAnyTrue() { for(std::list< boost::shared_ptr<Foo> >::iterator i =
Is this the right way to use delete[] operator? int* a=new int[size]; delete[] a;
INFO I'd like to use boost::function to pass callback as parameter, like this way:
Is this the right way to use mysql_real_escape_string ? I was using $GET but
Is this the right way to use C++11 rvalue-references and move semantics to implement
Is this the right way to do a nested navigation? <dl> <dt>Struktur</dt> <dd> <ul
Is this the right way to return an object from a function? Car getCar(string
could someone tell me if this is the right way to do bidirectional mapping?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.