1. A default (no-parameter) constructor is for creating an object with default state. 1.1 That is, with member data set to default values. 1.2 The principle behind this is that (by design) such objects can be used immediately after creation. 1.3 Note that it must make sense to construct an object with default state. 1.4 There are 2 points to note about code like the following :
1.4.1 This indicates that we want to create an object "instance" of type "some_class" and that "instance" should have default state. 1.4.2 If it is deemed acceptable that a "some_class" object, in default state, has member data that can be set to default values, then it does make sense to have "instance" in default state. 1.4.3 The compiler requires the definition of a default constructor in order to successfully compile code like the above.
2. If there are no constructor functions at all for "some_class", it means that we want the compiler to create the default constructor for us. This is not always a good idea because an unsatisfactory constructor may be created for our code.
3. If we have at least one non-default constructor for "some_class", but no default constructor for the class, e.g. :
This indicates the following :
3.1 That by design a "some_class" object has no default state. 3.2 That in order to be constructed properly, parameters must be given in order to sensibly initialize the state of the object. 3.3 In the case of "some_class", it means that parameter "i" is used to somehow initialize an instance to a meaningful state. 3.4 In this case, the compiler will not generate any default constructor for "some_class" because by design the class must not have one.
3.2 That in order to be constructed properly, parameters must be given in order to sensibly initialize the state of the object.
3.3 In the case of "some_class", it means that parameter "i" is used to somehow initialize an instance to a meaningful state.
Now, it can happen that the parameter(s) of a constructor be meaningless and would not sensibly initialize the object.
In this case, we can also throw an exception inside the constructor in order to indicate to the client code that something has gone wrong. The client code must of course contain appropriate try/catch blocks.
Last Visit: 31-Dec-99 19:00 Last Update: 27-Feb-17 20:21