0

What is the meaning of the following excerpt:

  : program_(program), max_iters_(max_iterations), num_iters_(0) 

in this piece of code:

Search::Search(const string& program, int max_iterations)
  : program_(program), max_iters_(max_iterations), num_iters_(0) 
{
  max_branch_ = 0; 
 // ...
}
Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
NewMrd
  • 431
  • 3
  • 13
  • 1
    See my answer to [What does a colon following a C++ constructor name do?](http://stackoverflow.com/a/1272707/96780) – Daniel Daranas Jul 22 '13 at 10:47
  • See this link http://stackoverflow.com/questions/9903248/initializing-fields-in-constructor-initializer-list-vs-constructor-body – Rahul Pandey Jul 22 '13 at 10:59
  • 3
    Just curious, but where are people learning C++ today. I can't imagine any introductory text which didn't explain this. – James Kanze Jul 22 '13 at 10:59
  • @JamesKanze my first contact with c++ was at the university, and the script did not cover this. i ran into it when i first had a class without a default constructor. – user2573221 Jul 22 '13 at 11:04
  • @JamesKanze Exactly, exactly. I'm wondering too. When people ask questions like "why `"foo"[0] = 'a';` segfaults", then I always ask back "what did your beginner C textbook says about it?"... –  Jul 22 '13 at 11:06
  • 1
    i found my answer in [What does a colon following a C++ constructor name do](http://stackoverflow.com/questions/1272680/what-does-a-colon-following-a-c-constructor-name-do) ? tnx @DanielDaranas. – NewMrd Jul 22 '13 at 11:15
  • @H2CO3 That one's almost excusable. It was guaranteed to work in pre-ISO C (and described as guaranteed to work in K&R-I). – James Kanze Jul 22 '13 at 11:55

3 Answers3

4

What you are referring to is called initializer list. A class or struct can initialize member variables using this list in its constructors.

Example:

struct foo
{
    foo() : member_(0) {}

    private:
        int member_;
};

The initialization list is especially important if you have members that have no default constructor - because when you reach in the body of the constructor, every member will have been created. If you did not choose a proper constructor for this member, compilation will fail.

Example:

struct bar { bar(int) {} };

struct foo
{
    foo() 
        : bar_(0) // does not compile without this line
                  // because the compiler will try to default
                  // construct otherwise
    {}

    private:
        bar bar_;
};
user2573221
  • 494
  • 4
  • 13
1

That is a constructor initialization list. Presumable, Search is a class with data members program_, max_iters and num_iters.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
0

First, it's not a function declaration but a class constructor implementation

The class Search declared before a ctor with 2 parameters and now your are in front of its implementation where the parameters are use to initialize data member.

alexbuisson
  • 7,699
  • 3
  • 31
  • 44
  • I am nitpicking, but a constructor is a function. It's mentioned in the C++11 standard within 12.1 as a special function, but a function nonetheless. – user2573221 Jul 22 '13 at 10:53
  • why "num_iters_" is initilized in initialization list but "max_branch_" is initilized in body? – NewMrd Jul 22 '13 at 10:56