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);
}
  1. A. Compile-time error: Logger has no usable constructor
  2. B. Ctor Used
  3. C. Compile-time error: deleting the copy constructor also deletes the default one
  4. 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

Exams that ask this topic