Price your future right. 60% off with code BLACKSCHOLES at USD checkout. Offer ends in 8d 09h 54m 24s8 days 9 hours 54 minutes 24 seconds.
Price your future right. 60% off with code BLACKSCHOLES at USD checkout. Offer ends in 8d 09h 54m 24s8 days 9 hours 54 minutes 24 seconds.

Sign in to watch this lesson
Classes and Resource Ownership · 3:38
The default copy of a class copies its members one by one, so two Vectors end up sharing one heap block: a write through one shows up in the other, and at the end of the scope the block is deleted twice and the program aborts. We fix it with a copy constructor and a copy assignment that duplicate the block, allocate before deleting so a failure leaves the object untouched, and finish with the copy-and-swap idiom.