Question discussion

What is the result of compiling and running the following C++ program?#include using namespace std; class A { public: explicit A(int x) { c…

2568Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the result of compiling and running the following C++ program?

#include <iostream>
using namespace std;
class A {
public:
    explicit A(int x) { cout << x; }
};
int main() {
    A a = 5;
    return 0;
}
  • A5
  • BCompilation error
  • C0
  • DNothing is printed
Correct answer: B

Explanation

An explicit constructor cannot be used for implicit conversion, so the copy-initialisation A a = 5 is rejected, whereas A a(5) or A a{5} would be accepted and would print 5.

Discussion 0

Nobody has replied yet. Start the discussion!