Constructor

Can someone pls explain to me what this constructor actually means
MinLeftistTree(LeftistNode *init=0) root(init){};
This is a constructor for the class MinLeftistTree. It has 1 parameter (init), it has a default value assigned to (0). It initializes the root member variable with init, using the initializer list syntax.
You can call it like that:
1
2
3
4
MinLeftistTree m; //Same as MinLeftistTree m(0);
//or
LeftistNode *n = /**/;
MinLeftistTree m(n);
Ok thanks for that. I wanted to know how the root(init) part works. The compiler threw an error when i created an object of this class
It is basically the same* as simply assigning root to init in the constructor body. (root = init;).

*For built in types, the overhead of calling it in the constructor body instead of the initializer list is optimized away by the compiler. For other types, it is recommended to use initializer lists, while for initializing const members, you have to use initializer list.
Thanks that works :)
Topic archived. No new replies allowed.