[c++] C++, What does the colon after a constructor mean?

Possible Duplicates:
Variables After the Colon in a Constructor
C++ constructor syntax question (noob)

I have some C++ code here:

class demo 
{
private:
    unsigned char len, *dat;

public:
    demo(unsigned char le = 5, unsigned char default) : len(le) 
    { 
        dat = new char[len];                                      
        for (int i = 0; i <= le; i++)                             
            dat[i] = default;
    }

    void ~demo(void) 
    {                                            
        delete [] *dat;                                           
    }
};

class newdemo : public demo 
{
private:
    int *dat1;

public:
    newdemo(void) : demo(0, 0)
    {
     *dat1 = 0;                                                   
     return 0;                                                    
    }
};

My question is, what are the : len(le) and : demo(0, 0) called?

Is it something to do with inheritance?

This question is related to c++

The answer is


This is called an initialization list. It is for passing arguments to the constructor of a parent class. Here is a good link explaining it: Initialization Lists in C++


You are calling the constructor of its base class, demo.


It means that len is not set using the default constructor. while the demo class is being constructed. For instance:

class Demo{
    int foo;
public:
    Demo(){ foo = 1;}
};

Would first place a value in foo before setting it to 1. It's slightly faster and more efficient.


It's called an initialization list. It initializes members before the body of the constructor executes.


It's called an initialization list. An initializer list is how you pass arguments to your member variables' constructors and for passing arguments to the parent class's constructor.

If you use = to assign in the constructor body, first the default constructor is called, then the assignment operator is called. This is a bit wasteful, and sometimes there's no equivalent assignment operator.