ProgrammingHardWipro · Cognizant pattern
What is printed?
What is printed?
struct Logger {
Logger(const Logger&) = delete;
Logger& operator=(const Logger&) = delete;
Logger() { cout << "Ctor "; }
};
void use(Logger &l) { cout << "Used "; }
int main() {
Logger l1;
use(l1);
}- A. Compile-time error: Logger has no usable constructor
- B. Ctor Used
- C. Compile-time error: deleting the copy constructor also deletes the default one
- D. Ctor only, use(l1) fails to compile
Correct answer: B. Ctor Used
Deleting the copy constructor and copy assignment only disables copying; the implicitly-usable default constructor Logger() is explicitly provided and still works. l1 is default-constructed (Ctor), and passing it by reference to use() never invokes the copy constructor, so it compiles fine and prints Ctor Used.
More OOP with C++ and Java questions
- What is printed? java class Base { Base() { System.out.print("Base "); } } abstract class Middle extends Base { Middle() {…
- What is printed? cpp class Matrix { public: Matrix operator(const Matrix &o) const { cout << "mul "; return Matrix(); } Matrix…
- What is printed? java class Base { public Base() { System.out.print("Base() "); } } class Sub extends Base { public Sub() { super()…
- What is printed? cpp struct Shape { virtual ~Shape() = default; virtual double area() const { return 0; } }; struct Circle : Shape {…
- What is printed? java class Counter { private int count; public synchronized void inc() { count++; } public int get() { return count; } }…
- What is printed? java class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } @Override public boolean equals(Object o) {…