Quote:Original post by garyfletcher
Do you mean just with Constructors/Destructors or with all fumction protoptypes?
Just with the constructors. Understand what is going on here. When you use the initializer list, that data is used to initialize data before the constructor is called.
class Point { int x; int y; int z; Point() : x(0), y(0), z(0) { // x, y and z are 0 before this code gets called. }};
Now why is it important. One reasons it that initializing data this way can speed things up. This is not always the case, but its good to remember. The other reason is to pass things to the base class constructor. When you create an object, notice the order in which they are created and destroyed.
#include <iostream>class A {public: int num; A( int n ) : num(n) { std::cout << "A()" << std::endl; } ~A(){ std::cout << "~A()" << std::endl; }};class B : public A{public: B() : A(23) { std::cout << "B()" << std::endl; } ~B(){ std::cout << "~B()" << std::endl; }};int main(){ B b; std::cout << b.num << std::endl; return 0;}// outputA()B()23~B()~A()
Objects are created in one order, and deleted backwards. Before the constructor for B is call, A must be created. But A needs an int? So use the initializer list the pass the number before the B constructor code is called.
Hope I helped, not made it worse.