What is the weird colon-member (“ : “) syntax in constructor?
It’s a member initialization list.
That’s constructor initialization. It is the correct way to initialize members in a class constructor, as it prevents the default constructor being invoked.
programs :
type 1: what we do normally in a program is,
struct TestStruct {
int id;
TestStruct()
{
id = 42;
}
};
type 2: we write the same thing using the initailizers list,
struct TestStruct {
int id;
double number;
TestStruct() : id(42) {}
};
One more example :
Point(int i = 0, int j = 0):x(i), y(j) {}
The above use of Initializer list is optional as the constructor can also be written as:
Point(int i = 0, int j = 0) {
x = i;
y = j;
}
CPP supports the Initializer list.Java doesn’t support initializer list because,
In C++, initializer lists are necessary because of a few language features that are either not present in Java or work differently in Java:
- const: In C++, you can define a fields that are marked const that cannot be assigned to and must be initialized in the initializer list. Java does have final fields, but you can assign to final fields in the body of a constructor. In C++, assigning to a const field in the constructor is illegal.
- References: In C++, references (as opposed to pointers) must be initialized to bind to some object. It is illegal to create a reference without an initializer. In C++, the way that you specify this is with the initializer list, since if you were to refer to the reference in the body of the constructor without first initializing it you would be using an uninitialized reference. In Java, object references behave like C++ pointers and can be assigned to after created. They just default to null otherwise.
- Direct subobjects. In C++, an object can contain object directly as fields, whereas in Java objects can only hold references to those objects. That is, in C++, if you declare an object that has a string as a member, the storage space for that string is built directly into the space for the object itself, while in Java you just get space for a reference to some other String object stored elsewhere. Consequently, C++ needs to provide a way for you to give those subobjects initial values, since otherwise they’d just stay uninitialized. By default it uses the default constructor for those types, but if you want to use a different constructor or no default constructor is available the initializer list gives you a way to bypass this. In Java, you don’t need to worry about this because the references will default to null, and you can then assign them to refer to the objects you actually want them to refer to. If you want to use a non-default constructor, then you don’t need any special syntax for it; just set the reference to a new object initialized via the appropriate constructor.
- In the few cases where Java might want initializer lists (for example, to call superclass constructors or give default values to its fields), this is handled through two other language features: the superkeyword to invoke superclass constructors, and the fact that Java objects can give their fields default values at the point at which they’re declared. Since C++ has multiple inheritance, just having a single super keyword wouldn’t unambiguously refer to a single base class, and prior to C++11 C++ didn’t support default initializers in a class and had to rely on initializer lists.
if you like the article / found it useful please take a few moments to clap for it. THANKYOU