66.2k views
2 votes
Given a Test object with a default constructor, copy constructor, and assignment operator defined: What does the following code invoke after the initial construction of both.

Test t1, t2;
t2 = t1;

a) Copy constructor
b) Assignment operator
c) Default constructor
d) None of the above

User BreenDeen
by
8.1k points

1 Answer

2 votes

Final answer:

The code 't2 = t1;' after the construction of 'Test t1, t2;' invokes the assignment operator, not the copy constructor. The assignment operator is used because t2 has already been created and is being assigned the value of t1.

Step-by-step explanation:

In the given code Test t1, t2; t2 = t1;, after the initial construction of t1 and t2 objects, the operation t2 = t1 invokes the assignment operator.

The default constructor is called when the objects t1 and t2 are initially declared. The copy constructor would be invoked if we were creating a new object as a copy of an existing object, for example, Test t3 = t1;. However, since t2 is already created, and we are simply assigning the value of t1 to t2, the assignment operator is what gets called in this scenario.

Answer: b) Assignment operator

User GuiDupas
by
7.3k points