Doubt in the class structure

1
2
3
4
5
6
7
class Lista {
	int array[100];
	int cont;
	public:
	Lista(): cont(0) {}
	~Lista() {}
};


What's the meaning of the : cont(0) {}?
Last edited on
: cont(0)

Assigns the value of 0 to int variable cont.

I don't really see why you are using that, becaues this method of assgning values to class variables. Is used in two situations,

Situation 1: When you want your constructor function to initilize const variables of class

Situation 2: Is used during Inheritance, when the inheriting class wants to initilize private variabes of base class.


Last edited on
it's in a question here to transform the program structure using inheritance concepts.
It's an initialization list. It is used to initialize the member cont. Alternatively, the member could be default constructed and assigned in the body of the constructor. The initialization list is more efficient, performing one operation rather than two.

If a class contains a reference member, it must be initialized in this manor because references must always be initialized and cannot be reassigned.
Last edited on
Topic archived. No new replies allowed.