EffectiveC++ Item25测试
test case1:
#include <iostream>
#include<vector>
#include <string>
#include <boost/foreach.hpp>
#include <boost/shared_ptr.hpp>
using namespace std;
class Widget
{
public:
Widget(){}
Widget(int aData, const string &aStr):m_Data(aData), m_Str(aStr){}
Widget(const Widget& lRhs):m_Data(lRhs.m_Data),m_Str(lRhs.m_Str)
{
}
Widget& operator=(const Widget& lRhs)
{
m_Data = lRhs.m_Data;
m_Str = lRhs.m_Str;
return *this;
}
void swap(Widget & theOtherA)
{
using std::swap;
Widget lTemp = theOtherA;
theOtherA = *this;
*this = lTemp;
}
friend ostream& operator<<(ostream &aCout, const Widget &aWidget);
private:
int m_Data;
string m_Str;
};
ostream& operator<<( ostream &aCout, const Widget &aWidget )
{
aCout << "Data: " << aWidget.m_Data
<< "Str: "<< aWidget.m_Str << endl;
return aCout;
}
namespace std
{
template <>
void swap <Widget> (Widget & a,Widget &b)
{
a.swap(b);
}
};
int main()
{
Widget lWidget1(1,"a");
Widget lWidget2(2,"b");
cout << "Orgin is : " << endl
<< "lWidget1 :" << lWidget1
<< "lWidget2: "<< lWidget2;
lWidget1.swap(lWidget2);
cout << "After swap : " << endl
<< "lWidget1 :" << lWidget1
<< "lWidget2: "<< lWidget2;
return 0;
}

浙公网安备 33010602011771号