Reading notes _EFFECTIVE_C++_ clause 25: Consider writing out a swap function that does not throw an exception

Source: Internet
Author: User

The operator= of the previously called object is to do so

    void Swap (a& x)    {        Std::swap (A, x.a);    }    Aoperator= (const a& a)    {        = A;        Swap (temp);         return *this;    }

The code above looks a bit cumbersome, but it's a good idea.

We can make our swap special in Std.

classa{Private:    intA; Public:    voidSwap (a& x)//prevent written friend, we give him an interface{Std::swap (A, x.a); } A (intx): A (x) {} A&operator=(Consta&a) {a temp=A; usingStd::swap; Swap (* This, temp); return* This; }    voiddisplay () {cout<< a <<Endl; }};namespacestd{//template<> The book, but I found that I could not    voidSwap<a> (a &s1, A &s2)//This <A> is a special, and heavy-duty parted {S1.swap (S2); }}intMain () {A A (1); A B (2);    B.display (); b=A; B.display ();}

When class is a template, then again.

Reading notes _EFFECTIVE_C++_ clause 25: Consider writing out a swap function that does not throw an exception

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.