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
}- A. L3 only
- B. L2, L3 and L4
- C. L3 and L4
- 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
- Which line fails to compile? cpp struct P { const int id; }; int main() { P a{1}, b{2}; P c = a; // L1 a = b; // L2 }
- How many of these statements about implicitly generated special members in C++11 and later are correct? (i) If a class declares a copy…
- What happens when this is compiled? cpp struct A { A(int) { } }; int main() { A a; }
- What is printed? cpp struct B { virtual void f() { cout << "B"; } }; struct D : B { void f() override { cout << "D"; } }; int main() {…
- What is printed? cpp template <class T> struct Base { void hi() { staticcast<T>(this)->impl(); } void impl() { cout << "B"; } }; struct X…
- What happens? cpp template <class T> T mx(T a, T b) { return a > b ? a : b; } int main() { cout << mx(3, 7); // L1 cout << mx(1, 2.5); //…