Discussion
Using a constructor to specify the type conversion is convenient but has implications that can be undesirable in :
(i) There can be no implicit conversion from a user defined type to basic type.
(ii) It is not possible to specify a conversion from a new type to an old one without modifying the declaration for the old one.
(iii) It is not possible to have a constructor with a single argument without also having a conversion.
(iv) A user defined conversion is implicitly applied only if it’s unique.
Answer: B
The undesirable implications are (i), (ii) and (iii).
Bjarne Stroustrup notes that a constructor taking one argument is a handy way to define a conversion, but it has limits. It cannot give an implicit conversion from a user-defined type to a basic type, since basic types are not classes (i). It cannot specify a conversion from a new class to an older one without changing the old class's declaration (ii). And, before the keyword explicit, every single-argument constructor also acted as an implicit conversion, wanted or not (iii). Conversion operators were introduced to deal with the first two.
Statement (iv) is a language rule, that an implicit user-defined conversion is used only when it is unambiguous, which prevents surprises rather than causing them. So options including (iv) are wrong.
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development