1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. Which signature correctly declares a copy constructor for…

Added 30 Nov -0001

Which signature correctly declares a copy constructor for class Widget in C++?

  1. Widget(Widget other);
  2. Widget& operator=(Widget other);
  3. void Widget(const Widget& other);
  4. Widget(const Widget& other); Correct answer

Correct answer

D. Widget(const Widget& other);

14 views

MCQSEXAM

Explanation

A copy constructor builds a new object from an existing one and takes a reference, conventionally const, as in Widget(const Widget&). It must take a reference because by-value would require another copy, a circular requirement the language forbids. The signature with operator= is copy assignment, which runs on an already-constructed object and must release existing resources first.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is D? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C++ and Object-Oriented Programming MCQs

Practise all C++ and Object-Oriented Programming MCQs

Similar MCQs