+ 1
Can anyone explain the copy& swap idiom wrt assignment operator overloading in C++?
I've been searching for it and couldn't find an explanation that I could understand. Please explain it to me, and if possible, use C++ for examples (if any). Thank you =)
9 Respuestas
+ 2
Rishi right.
But the problem with this approach is that, you would still have to perform a manual cleanup of the object ( otherwise you can easily get bad stuff like memory leaks if any of the data member points to dynamically allocate memory )
In copy and swap idiom, we do the same except we make use of class's destructor to perform that cleanup, you can simply do that by "swapping" the data with the "copy" of the object you have and program will implicitly call destructor on that copy when it goes out of scope, performing the cleanup for you .
A typical overload of copy assignment operator looks something like this :-
```
A& operator= ( A other )
{
swap( *this, other );
return *this;
}
```
+ 3
Arsenic yes I got it! I've seen some stuff about std:swap and std::move and I think I got it. Thank you so much!
+ 2
Rishi yes, you got that right
" i) Swapping doesn't do anything with memory allocation and stuff, It just swaps the names of the two variables."
You can also think of it this way, but swap function actually swaps the contents of both objects, so when the scope of "other" ends, the destructor performs cleanup on the contents of what was earlier contents of "*this".
+ 1
Your question is not clear rewrite again
+ 1
Rishi Say you want overload assignment operator of a class. How would you do it ?
+ 1
Arsenic It's so simple. I would define it as a friend function and I'll take the object to be copied as parameter. Then I'll assign every single member of the parameter to the current object.
And also, to prevent self assignment issue, I'll check if the addresses of both the objects are same, if they're not, only then I'll do the copy. Else I'll return "*this" (object on which the copies have to be made) :)
+ 1
Arsenic oooh. I think I got almost everything of what you told. Let me explain and say if I'm wrong. To prevent the issue, we do the following steps:
1)We get the copy of the object to be copied ("copy" part of the copy and swap idiom). This prevents any memory allocation error beforehand and so that if there is any memory allocation error, we would have got it before changing the target object's state.
2) Now that we have allocated memory ready, we swap the target object and the copy object ("swap" part of the copy and swap idiom). This does two things here.
i) Swapping doesn't do anything with memory allocation and stuff, It just swaps the names of the two variables.
ii) Now after step 1, we have the target copied with the object to be copied and our stuff went perfectly. Now the function exits. Before it exits, it'll call the destructor for "other" which deallocates it(in our example). Since we swapped "*this" and "other", the destructor actually deallocates the "*this" which is the target..
+ 1
Arsenic continuation:
.....which is the target object. So now we have successfully deallocated the targets memory (after copying, unlike in other methods where we deallocated the target and then do the allocation and copy stuff).
If this explanation is correct, then I understood everything that you tried to convey. Thank you so much ^-^
0
A.S. I have edited it, see now