ProgrammingVery hardProduct-company placement · C++

Which lines fail to compile?

Which lines fail to compile?
struct E { explicit E(int) { } };
int main() {
    E a(1);      // L1
    E b{1};      // L2
    E c = 1;     // L3
    E d = {1};   // L4
}
  1. A. L3 only
  2. B. L2, L3 and L4
  3. C. L3 and L4
  4. D. L4 only

Correct answer: C. L3 and L4

An explicit constructor is usable only in direct-initialisation: parentheses or braces without =. Both = forms are copy-initialisation (L4 is copy-list-initialisation), which cannot use an explicit constructor.

More OOP with C++ and Java questions

Exams that ask this topic