0

If I have a binary search tree header file BST.h and inside it I have:

template <class type>
struct bstNode
{
    type info;
    bstNode * lLink;
    bstNode * rLink;
};

And then I have an AVL.h header file and I want to use the bstNode struct in my AVL.h file like this:

template<class type>
struct avlNode
{
    bstNode<type> bstN;
    int height;

    avlNode(const type & data, bstNode<type> *ll, bstNode<type> *rl, int h = 0)
    :    //how would the initialization go here?
};

My question is how would I initialize the avlNode constructor with the initializer list syntax? I'm not sure how to access the members in bstN.

I can do it with the traditional definition outside the struct:

template<class type>
avlNode<type>::avlNode(const type & data, bstNode<type> *ll, bstNode<type> * rl, int h)
{
    bstN->info = data;
    bstN->lLink = ll;
    bstN->rLink = rl;
    height = h;
}

But I would Like to learn the syntax for the member initialization list when it comes to using an object (bstN) from another class/struct.

David Velasquez
  • 2,346
  • 1
  • 26
  • 46

3 Answers3

3

You don't access the members directly in the initialization list. That's why you would make a constructor that takes as many arguments as you need and pass values to it in the initialization list.

For example,

template <class type>
struct bstNode
{
    type info;
    bstNode * lLink;
    bstNode * rLink;

    bstNode( const type &data, bstNode *left, bstNode *right ) :
        info( data ), bstNode( left ), bstNode( right )
    {
    }
};

Then call this constructor in your avlNode.

template<class type>
avlNode<type>::avlNode(const type & data, bstNode<type> *ll, bstNode<type> * rl, int h) :
    bstN( data, ll, rl ), height( h )
{
}
aslg
  • 1,966
  • 2
  • 15
  • 20
2

bstNode is currently an aggregate, so you may do directly

template<class type>
avlNode<type>::avlNode(const type& data, bstNode<type>* ll, bstNode<type> * rl, int h) :
    bstN{data, ll, rl}, height{h}
{
}

Providing bstNode constructor may be also an option.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
0

you can define an construct function in bstNode

 template <class type>
    struct bstNode
    {
        type info;
        bstNode * lLink;
        bstNode * rLink;
        bstNode(const type & data = type(), bstNode<type> *ll =0, bstNode<type> *rl =0):info(data),llike(ll),rlick(rl){}
    };
template<class type>
struct avlNode
{
    bstNode<type> bstN;
    int height;

    avlNode(const type & data, bstNode<type> *ll, bstNode<type> *rl, int h = 0)
    :    bstN(data,l1,rl),height(h)
};
kiviak
  • 1,083
  • 9
  • 10