1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. A single-argument constructor is marked explicit. What…

Added 30 Nov -0001

A single-argument constructor is marked explicit. What does that prevent?

  1. Direct-initialisation, so objects can only be created through a factory function
  2. Copy construction, so the type can no longer be stored inside standard containers
  3. Aggregate initialisation, since the class is no longer an aggregate after the change
  4. 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

MCQSEXAM

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

Preparing for an exam?

Doubts & discussion

Not sure why the answer is D? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C++ and Object-Oriented Programming MCQs

Practise all C++ and Object-Oriented Programming MCQs

Similar MCQs