-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy path13.12.txt
20 lines (17 loc) · 975 Bytes
/
13.12.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Exercise 13.12: How many destructor calls occur in the following code fragment?
bool fcn(const Sales_data *trans, Sales_data accum)
{
Sales_data item1(*trans), item2(accum);
return item1.isbn() != item2.isbn();
}
A destructor call occurs every time a class object is destroyed. trans and
accum are temporary copies. They are destroyed when they fall out of scope at
the end of the code. trans, however, does not invoke a destructor because it is
a built-in type. item1 and item2 are local class objects. They, too, are
destroyed when they fall out of scope. Like, accum, a call to thej destructor
occurs when the are destroyed. The order of their destruction is the inverse of
their construction: item2, item1, accum. Each object has a string member.
Therefore, Sales_data destructor calls the string destructor to handle the
deletion. Thus, in total, 6 destructor calls are made: 3 Sales_data destructor
calls and 3 string destructor calls.
By Faisal Saadatmand