copy and swap idiom

Hi guys I am trying to get to grips with move semantics I must say c++11 is much more complex then previous versions well it is in my opinion I know the use of things like smart pointers and other things can save programs from bugs but I feel like it just over complicates things

anywho here is the tutorial I am following https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom

which discusses the copy and sap idiom,I don't get this idiom.Why would we need to use it? why couldn't we just use the operator= like the copy constructor,sure we have duplicated code but I can't see the issue with this,

also it looks like we are swapping the contents of the objects,so this means that if we sat A = B then A will equal B and B will equal A but A's values may be null,even at that we change the integrity of B which I'm sure we don't want to do.

is there something I'm missing?

thanks



** UPDATE ** I think I now understand what is going on,but I will leave this question here so if anybody else stumbles upon it they will have a reference

I am not great at explaining yet so if anyone wants to try explain it in their own words that would be great,if not I will come back and do my best with an explanation.

I will also add why is the copy and swap idiom more efficient than the traditional way of copying with the = operator,or is it even more efficient?

thanks
Last edited on
Topic archived. No new replies allowed.