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?
A
Copy-initialisation uses the copy constructor; assignment to an existing object uses the copy assignment operator
Correct answer
B
Both use the copy constructor, since the equals sign always means construction even for a live object
C
Both use the copy assignment operator, since the equals sign always means assignment whatever the surrounding context
D
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
⚔️
Challenge a friend on C++ and Object-Oriented Programming
Same MCQs, timed, live leaderboard. Send the link on WhatsApp – no sign-up needed.
Create challenge
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
Doubts & discussionNot sure why the answer is A? Ask here and get an explanation.
Ask a doubt about this MCQ
‹ Previous MCQ On an empty std::vector<int> v, what do v.reserve(100) and v.resize(100) each produce? Next MCQ › In C++11, declaring a user-defined destructor, copy constructor or copy assignment operator suppresses which implicitly generated members?