[try Beta version]
Not logged in

 
 
Single question about a common abstract data type

Jan 27, 2011 at 7:29pm
The ADT is a node for an AVL tree. I understand its basic makeup--data/object, left node pointer, right node pointer, and height (for balancing). The class that holds this private struct is a template. However, there's a line in my text that I'm not terribly familiar with; it looks like a constructor of sorts that one would use when using inheritance. Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
template <typename Comparable>
class AVLtree
{
.
.
.
private:
struct AVLNode
{
     Comparable element;
     AVLNode *left;
     AVLNode *right;
     int height;

     AVLNode( const Comparable & theElement, AVLNode *lft, AVLNode *rht)
        : element( theElement), left( lft ), right( rht ) {  }
};


The last 2 lines of actual code have me a bit confused. The calls to "element", "left", and "right" look like actual function calls (whose functions really aren't important for this question, I believe).

Can someone please explain those last 2 lines to me? Are they function objects?
Jan 27, 2011 at 8:08pm
lines 15, 16 are a constructor of an AVLNode object.
a constructor of class CLASS looks like this: CLASS( arguments ) : initializer_list { the_remaining_construction; }
the initializer list (which together with the ':' is an optional part) is a way to call the constructors of members.

for example
1
2
3
4
5
6
7
8
struct A{
   int x;
   A() : x(5) {}
};
int main(){
   A a;
   return a.x;
}

would return 5 since the constructor of A initialized x to 5 (by calling integer's constructor). note that A() { x = 5; } would have done the same. in some cases using an initializer list may be faster though.
Last edited on Jan 27, 2011 at 8:09pm
Jan 28, 2011 at 5:01am
You da man. Thanks so much!
Topic archived. No new replies allowed.