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 6690155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:36:13+00:00 2026-05-26T05:36:13+00:00

I’m writing a tree-like container, where each node has a list with branches/subtrees, currently

  • 0

I’m writing a tree-like container, where each “node” has a list with branches/subtrees, currently my head looks like:

class _tree {
public:
    typedef _tree* tree_ptr;
    typedef std::list<_tree> _subTreeTy;

    explicit _tree(const _ValTy& v, const _NameTy& n); //create a new tree
    _tree(const _ValTy& v, const _NameTy& n, tree_ptr _root); 
         //create a new tree and add it as branch to "_root".

    ~_tree();

    void add_branch(const _tree& branch); //add by copy
    void add_branch(_tree&& branch); //add by move
private:
    _subTreeTy subtrees;
    _ValTy value;
    _NameTy name;
};


_tree::_tree(const _ValTy& v, const _NameTy& n, tree_ptr _root)
    : root(_root),
    value(v),
    name(n)
{
    _root->add_branch(*this); //not rvalue(???)
}

Now the second constructor would create a tree inside _root – however how does this work with calling (ignore private violation):

_tree Base(0,"base");
_tree Branch(1, "branch", &Base);
Base.subtrees.begin()->value = 8;
std::cout << Branch.value;

How would I make it so that Branch & *Base.subtrees.begin() refer to the same node? Or should i go the other way. Use add_branch() to CREATE a branch/subtree?

  • 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-05-26T05:36:13+00:00Added an answer on May 26, 2026 at 5:36 am

    Move semantics is about moving the internals of an object, not the object (as a typed piece of memory) itself. It’s best to think about it in terms of values and invariants, since C++ still has value semantics even when moving is taken into account. This means:

    std::unique_ptr<int> first(new int);
    // invariant: '*this is either null or pointing to an object'
    // current value: first is pointing to some int
    assert( first != nullptr );
    
    // move construct from first
    std::unique_ptr<int> second(std::move(first));
    
    // first and second are separate objects!
    assert( &first != &second );
    
    // New values, invariants still in place
    assert( first == nullptr );
    assert( second != nullptr );
    
    // this doesn't affect first since it's a separate object
    second.reset(new int);
    

    In other words, while you can convert the expression *this to an rvalue by doing std::move(*this) what you want cannot be achieved right now since std::list<_tree> uses value semantics and _tree has value semantics itself. *Base.subtrees.begin() is a distinct object from Branch and as things are modifications to the former won’t affect the latter.

    Switch to reference semantics if that’s what you want (or need), for instance by using std::shared_ptr<_tree> and std::enable_shared_from_this (you’d then use _root->add_branch(shared_from_this()) inside the constructor). I wouldn’t recommend it though, this could get messy. Value semantics are very desirable in my opinion.


    With value semantics, using your tree could look like:

    _tree Base(0, "base");
    auto& Branch = Base.addBranch(1, "branch");
    

    That is, addBranch returns a reference to the newly constructed node. Sprinkling some move semantics on top:

    _tree Base(0, "base");
    _tree Tree(1, "branch); // construct a node not connected to Base
    auto& Branch = Base.addBranch(std::move(Tree));
    // The node we used to construct the branch is a separate object
    assert( &Tree != &Branch );
    

    Strictly speaking if _tree is copyable move semantics are not necessary though, Base.addBranch(Tree); would work too.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I would like to count the length of a string with PHP. The string
I want use html5's new tag to play a wav file (currently only supported

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.