Quote:
Originally Posted by bx.s
So I am assuming this is a constructor method. The syntax that I find confusing is the colon and list of private instance variables after the function name. My guess is that this is another way of setting initial values to these, but I want to check with somebody who knows before I proceed with this assumption.
|
Yes, this is how to initialize member variables of an instance. It calls the constructor for each of the members with the arguments that you give it.
If you do not do this, then members will be initialized with the default constructor (no arguments); which may not be desirable.
For class types, if you do not provide an initializer and assign something to it later, that is bad because it has to construct it using the default constructor, and then assign something else using the assignment operator.
Also, some types (references and const types) cannot be assigned to or copied to, and therefore must be initialized this way.
In Java, you did not have to deal with this because all values are either primitive types or reference types, and so don't need to be initialized specially.