What does it mean when we use a variable in C ++ as a function with a default value as its parameter?

I was reading a line of C ++ code. I came across a strange line of code where a variable was being used as a function with 0 as its parameter!

template <class T> class Stack {
    T data[50];
    int nElements;
public:
//This line is where the variable was used like a function!
    Stack() : nElements(0){}
    void push(T elemen);
    T pop();
    int tamanho();
    int isEmpty();
};

      

So what exactly does it mean when we have:   constructor: private variable (0) {}

This line of code was very strange to me! Thanks to

+3


source to share


3 answers


In the initializer_list of the constructor, the Stack

class member is nElements

initialized to null when each is created Stack

.



The meaning 0

here doesn't really matter, other than setting the initial cardinality for Stack

to zero as soon as it is created and empty.

+2


source


This is called an initializer list



+2


source


This is called an "initializer". It says to initialize the variable with the given value, and {} indicates that the constructor body is empty.

+1


source







All Articles