A single-argument constructor is marked explicit. What does that prevent?
A
Direct-initialisation, so objects can only be created through a factory function
B
Copy construction, so the type can no longer be stored inside standard containers
C
Aggregate initialisation, since the class is no longer an aggregate after the change
D
Implicit conversion from the argument type at call sites and in copy-initialisation
Correct answer
Correct answer
D. Implicit conversion from the argument type at call sites and in copy-initialisation
16 views
⚔️
Challenge a friend on C++ and Object-Oriented Programming
Same MCQs, timed, live leaderboard. Send the link on WhatsApp – no sign-up needed.
Create challenge
Explanation
explicit blocks implicit conversions and copy-initialisation such as Widget w = 5;, while direct-initialisation Widget w(5); still works. This prevents silent conversions in function arguments and operator overloads, a common bug with single-argument numeric wrappers. Since C++11 explicit also applies to conversion operators and to multi-argument constructors used in list-initialisation.
Reference
Asked in: NTS, PPSC IT, Campus Interviews
Doubts & discussionNot sure why the answer is D? Ask here and get an explanation.
Ask a doubt about this MCQ
‹ Previous MCQ A lambda captures a local int by reference with [&] and is stored in a std::function that outlives the enclosing function. What happens when it is invoked later? Next MCQ › An iterator it points into a std::map. The statement it = m.erase(it); is executed. What does it hold afterwards?