-2

Found this code in an Introduction to C++ course presentation. It is refering to working with classes as Data Types. The thing I don't understand is line 5, the Bounded_Stack function definition. What does the ":" means there and later the "stack_ (len), top_ (0)". I can understand basic C++ but have never encountered this sintax before.

The code:

#include "Vector.h"
template <class T>
class Bounded_Stack {
public:
Bounded_Stack (int len) : stack_ (len), top_ (0) {}
// . . .
private:
Vector<T> stack_;
int top_;
};
harogaston
  • 124
  • 1
  • 10
  • 2
    Read about [member initialization lists](http://en.cppreference.com/w/cpp/language/initializer_list). – Some programmer dude Mar 18 '15 at 16:56
  • I believe you need to read more about C++ in general. `Bounded_Stack(int)` is not a function in this context; in fact it doesn't have a return type. It is a constructor with initialization list. If you don't know what these terms mean you need a book or tutorial about C++ classes definitions. I always recommend Koenig's Accelerated C++ for a good intro. – Giovanni Botta Mar 18 '15 at 16:59
  • Thank you. I've found my answer already thanks to @zenith . I know it is a constructor I just expressed it loosely, led by my Spanish-molded mind. – harogaston Mar 18 '15 at 17:14

1 Answers1

1

It's called the constructor initialization list. It initializes the member variables of the class.

For example the Bounded_Stack(int) constructor in your example initializes the member stack_ to len and top_ to 0.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
  • No, it's called a "member initializer list". – Jerry Coffin Mar 18 '15 at 17:31
  • But "constructor initialization list" is more descriptive. "Member initializer list" sounds like an initializer list used to initialize a single member. – Emil Laine Mar 18 '15 at 17:35
  • The problem is that an "initialization list" is something else entirely (e.g., in `std::vector x{1, 2, 3, 4};`, the `{1, 2, 3, 4}` part is an initialization list). – Jerry Coffin Mar 18 '15 at 17:49
  • I thought those were called "initializer lists", probably because of the [`std::initializer_list`](http://en.cppreference.com/w/cpp/utility/initializer_list). Am I wrong? – Emil Laine Mar 18 '15 at 17:50
  • Both names are used for them. I'm less concerned about "initializer" vs. "initialization" than the "member" being there to distinguish the two. – Jerry Coffin Mar 18 '15 at 17:55