views:

181

answers:

3

Possible Duplicates:
C++ weird constructor syntax
Variables After the Colon in a Constructor
What does a colon ( : ) following a C++ constructor name do?

For the C++ function below:

cross(vector<int> &L_, vector<bool> &backref_, vector< vector<int> > &res_) : 

    L(L_), c(L.size(), 0), res(res_), backref(backref_) {

    run(0); 

}

What does the colon (":") tell the relationships between its left and right part? And possibly, what can be said from this piece of code?

+4  A: 

It's a member initialization list.

You're setting each of the member variables to the values in parentheses in the part after the colon.

John at CashCommons
@John: Thankyou
luna
You're welcome!
John at CashCommons
+4  A: 

This is a way to initialize class member fields before the c'tor of the class is actually called.

Suppose you have:

class A {

  private:
        B b;
  public:
        A() {
          //Using b here means that B has to have default c'tor
          //and default c'tor of B being called
       }
}

So now by writting:

class A {

  private:
        B b;
  public:
        A( B _b): b(_b) {
          // Now copy c'tor of B is called, hence you initialize you
          // private field by copy of parameter _b
       }
}
Artem Barger
@Artem: thank you, what is a "c'tor" please?
luna
c'tor == constructor
John at CashCommons
@John: Ok, see, it did save time
luna
+3  A: 

Like many things in C++, : is used for many things, but in your case it is the start of an initializer list.

Other uses are for example after public/private/protected, after a case label, as part of a ternary operator, and probably some others.

Brian R. Bondy
Note: subject was modified after my answer to be more specific.
Brian R. Bondy
@Brian: Thankyou
luna