0

What does the additional ':' mean in this function definition?

template <class T, int SIZE> 
class Buffer
{
 public: 
    Buffer();
 private: 
    int _num_items; 
};

template <class T, int SIZE> 
Buffer<T, SIZE>::Buffer() :
    _num_items(0) //What does this line mean?? 
{
   //Some additional code for constructor goes here. 
}
Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
EvanJ227
  • 37
  • 3

1 Answers1

2

This is how you can initialize member variables (and you should do that)

class Something
{
private:
  int aValue;
  AnotherThing *aPointer;

public:
  Something() 
   : aValue(5), aPointer(0)
  {
     printf(aValue); // prints 5
     if(aPointer == 0) // will be 0 here
       aPointer = new AnotherThing ();
  }
}

It's the initialisation list - the members will be initialized with the given value.

Stefan Falk
  • 23,898
  • 50
  • 191
  • 378
  • Thank you for your answer, so would this only apply to a templated class or can it apply to any class? – EvanJ227 Jun 16 '13 at 12:55
  • Any class can initialize its members that way. In your case its a template class but this initialisation list applies to all classes in C++. Just google `c++ constructor initialization list` – Stefan Falk Jun 16 '13 at 12:57
  • Thank you. This site is awesome. Some of this stuff i wouldnt even know where to start when it came to finding it. – EvanJ227 Jun 16 '13 at 13:03
  • You're welcome. stackoverflow truly rocks! If you want to accept an answer you can click the check mark on the left side so users can see that this question has got an answer btw :) – Stefan Falk Jun 16 '13 at 13:06
  • I was waiting because i guess that you have to wait for 10 minutes to select an answer. That set me on the right track. – EvanJ227 Jun 16 '13 at 13:12
  • Oh I see :) Well, have a nice programming! – Stefan Falk Jun 16 '13 at 13:16