1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. For objects a and b of class T, which special member is…

Added 30 Nov -0001

For objects a and b of class T, which special member is used by T b = a; and which by b = a; when b already exists?

  1. Copy-initialisation uses the copy constructor; assignment to an existing object uses the copy assignment operator Correct answer
  2. Both use the copy constructor, since the equals sign always means construction even for a live object
  3. Both use the copy assignment operator, since the equals sign always means assignment whatever the surrounding context
  4. T b = a; uses the default constructor followed by assignment, because b is an already constructed object

Correct answer

A. Copy-initialisation uses the copy constructor; assignment to an existing object uses the copy assignment operator

14 views

MCQSEXAM

Explanation

Copy constructor versus copy assignment depends on whether a new object is being created: T b = a; is copy-initialisation and calls the copy constructor, while b = a; on a live object calls operator=. A self-assignment check in operator= is essential when the class owns a resource, otherwise the resource can be released before it is copied. Move versions of both exist for rvalues.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is A? 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