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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:26:06+00:00 2026-05-26T14:26:06+00:00

I am writing a tree structure where I want to store different data in

  • 0

I am writing a tree structure where I want to store different data in leaf nodes to branch nodes. The idea is very similar to a dynamic programming method — with aggregate data being stored in the branches. Hence I have come up with (cut down for simplicity)

template<class L, class B> class node {
public:
    virtual ~node() {}
    // [...] visitor stuff
};

template<class L, class B> class branch : public node<L,B> {
public:
    template<class It>
    branch(It b, It e) {
        // Do something with the iterators;
        // decide if to create branches or leaves

        // Create the branch data
        b_ = new B(/* Unknown */);
    }
    ~branch() { delete b_; delete child_[0]; delete child_[1]; }

private:
    B* b_;  node<L,B>* child_[2];
};

template<class L, class B> class leaf : public node<L,B> {
public:
    leaf(L* l) : l_(l) {}
    ~leaf() {}

private:
    L* l_;
};

With example L and B structures being

struct myleaf   { int x; };
struct mybranch {
    mybranch(/* Unknown */) {
    // Apply a visitor to the child nodes to get sumx
    }
    int sumx;
    // Some other properties
};

My problem is how I can do this without encountering circular dependencies with B depending on node<L,B>. Alternatively, can I easily re architecture the code to avoid this issue entirely?

  • 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-26T14:26:06+00:00Added an answer on May 26, 2026 at 2:26 pm

    as far as I can see, there is no problem.

    It’s basically an example of CRTP, a fairly common pattern for template code in C++.

    Of course you can trip the compiler up so this won’t work, but at the basic level, there is nothing wrong with dependencies of the form class Derived : Base<Derived>

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

Sidebar

Related Questions

I'm writing an application that work with a tree data structure. I've written it
I am writing an entity that has a tree like data structure. I am
I'm writing a Java Tree in which tree nodes could have children that take
I'm writing a Perl script and would like to use a n-ary tree data
I'm writing a Ruby script and would like to use a n-ary tree data
I have a table in my database where I store a tree structure using
I'm writing an app that, at its heart, uses a hierarchical tree of nodes
I have a tree-like structure to my database in an app I am writing
I'm writing a program that builds up a tree structure made up of classes
I'm writing a ruby app that stores ancestry type data. I.e. Family tree. For

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.