Sie sind vermutlich noch nicht im Forum angemeldet - Klicken Sie hier um sich kostenlos anzumelden  
Sie können sich hier anmelden
Dieses Thema hat 0 Antworten
und wurde 13 mal aufgerufen
daijordansshi Offline

Beiträge: 5

13.03.2014 05:25
What is the copy antworten

What is the copy

Any class that manages a resource (a wrapper, like a smart pointer) needs to implement The Big Three. While the goals and implementation of the copy constructor and destructor are straightforward, the copy assignment operator is arguably the most nuanced and difficult. How should it be done? What pitfalls need to be avoided?

The copy and swap idiom is the solution, and elegantly assists the assignment operator in achieving two things: avoiding code duplication, and providing a strong exception guarantee.

Conceptually, it works by using the copy constructor's functionality to create a local copy of the data, then takes the copied data with a swap function, swapping the old data with D.J. Augustin Authentic Jersey the new data. The temporary copy then destructs, taking the old data with it. We are left with a copy of the new data.

In order to use the copy and swap idiom, we need three things: a working copy constructor, a working destructor (both are the basis of any wrapper, so should be complete anyway), and a swap function.

A swap function is a non throwing function that swaps two objects of a class, member for member. We might be tempted to use std::swap instead of providing our own, but this would be impossible; std::swap uses the copy constructor and copy assignment operator within its implementation, and we'd ultimately be trying to define the assignment operator in terms of itself!

(Not only that, but Dennis Rodman Authentic Jersey unqualified calls to swap will use our custom swap operator, skipping over the unnecessary construction and destruction of our class that std::swap would entail.)

The first is the self assignment test. This check serves two purposes: it's an easy way to prevent us from running needless code on self assignment, and it protects us from subtle bugs (such as deleting the array only to try and copy it). But in all other cases it merely serves to slow the program down, and act as noise in the code; self assignment rarely occurs, so most of the time this check is a waste. It would be better if the operator could work properly without it.

The second is that it only provides a basic exception guarantee. If new int[mSize] fails, this will have been modified. Our assignment operator effectively duplicates all the code we've already written elsewhere, and that's a terrible thing.

In our case, the core of it is only two lines (the allocation and the copy), but with more complex resources this code bloat can be quite a hassle. We should strive to never repeat ourselves.

(One might wonder: if this much code is needed to manage one resource correctly, what if my class manages more than one? While this may seem to be a valid concern, and indeed it requires non trivial try/catch clauses, this is a non issue. That's because a class should manage one resource only!)

As mentioned, the copy and swap idiom will fix all these issues. But right now, we have all the requirements except one: a swap function. While The Rule of Three successfully entails the existence of our copy constructor, assignment operator, and destructor, it should really be called "The Big Three and A Half": any time your class manages a resource it also makes sense to provide a swap function.

Xobor Ein Kostenloses Forum von
Einfach ein Forum erstellen