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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:57:43+00:00 2026-06-14T18:57:43+00:00

I want to define a ‘node’ class/struct and then declare a tree of these

  • 0

I want to define a ‘node’ class/struct and then declare a tree of these nodes in code in such a way that the way the code is formatted reflects the tree structure, and there’s not ‘too much’ boiler plate in the way.

Note that this isn’t a question about data structures, but rather about what features of C++ I could use to arrive at a similar style of declarative code to the example below.

Possibly with C++0X this would be easier as it has more capabilities in the area of constructing objects and collections, but I’m using Visual Studio 2008.

Example tree node type:

struct node
{ 
  string name;
  node* children;

  node(const char* name, node* children);
  node(const char* name);
};

What I want to do:

Declare a tree so its structure is reflected in the source code

node root =
  node("foo",
  [
    node("child1"),
    node("child2", 
    [
      node("grand_child1"),
      node("grand_child2"),
      node("grand_child3"
    ]),
    node("child3")
  ]);

NB: what I don’t want to do:

Declare a whole bunch of temporary objects/colls and construct the tree ‘backwards’

node grandkids[] = node[3] 
{
  node("grand_child1"),
  node("grand_child2"),
  node("grand_child3"
};

node kids[] = node[3]
{
  node("child1"),
  node("child2", grandkids) 
  node("child3")
};

node root = node("foo", kids);
  • 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-14T18:57:44+00:00Added an answer on June 14, 2026 at 6:57 pm

    If you don’t mind excessive copying of the nodes and using parenthesis () instead of square brackets [] then this should work.

    Actually you can avoid copying by storing pointers in the node_group rather than copies, but since this is friday afternoon and I’m very lazy, I’ll leave it to you.

    struct node
    {
        std::string name;
        std::vector<node> children;
    
        node(const char* n)
            :   name (n)
        {
        }
    
        node(const char* n, const class node_group& group);
    };
    
    struct node_group
    {
        std::vector<node> children;
    };
    
    node::node(const char* n, const class node_group& group)
        :   name (n)
        ,   children (group.children)
    {
    }
    
    node_group operator ,(const node& n1, const node& n2)
    {
        node_group group;
        group.children.push_back (n1);
        group.children.push_back (n2);
        return group;
    }
    
    node_group operator ,(const node_group& gr, const node& n2)
    {
        node_group group (gr);
        group.children.push_back (n2);
        return group;
    }
    
    
    int main ()
    {
        node root ("foo",
                    (node("child1"),
                    node("child2",
                        (node("grand_child1"),
                        node("grand_child2"),
                        node("grand_child3"))
                        ),
                    node("child3"))
                  );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to define reader macros in such a way that they affect only
I want to define a class that is able to populate itself reading from
I want to define some properties on a class using the [Indexable()] attribute in
I want to define a class that supports __getitem__ , but does not allow
I want to define operators for a class instance within a class function, like
I want to define a boost fusion::vector in my class with the size defined
I want to define a 2D (numpy) array such that cell(row,col) == row+col (or
Here's the scenario: I want define a base class that allocate some buffer once
I want to define a class like this class HttpRestAccessor { public: IResource& UpdateResource(string&
I want define schema in each xml file that I marshalling. And then get

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.