Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

I was reading a C++ code line. I encountered a weird code line where a variable was used as a function with a 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 does exactly mean when we have: the constructor : private variable (0){}

This code line was very weird for me! Thanks

like image 521
Amir Jalilifard Avatar asked Dec 04 '25 10:12

Amir Jalilifard


1 Answers

In the initializer_list of Stacks constructor, class member nElements is initialized with a value of zero upon creation of each Stack object.

The value 0 does not have any special meaning here, other than setting the initial number of elements for the Stack to zero as soon as it gets created and is empty.

like image 175
Pixelchemist Avatar answered Dec 07 '25 00:12

Pixelchemist