#include <vector>
#include <iostream>
using namespace std;
class CSomeClass
{
protected:
vector<int> * lpData;
public:
CSomeClass();
void SwapData(CSomeClass &pCopy);
void SetItemCount(int count= 0);
size_t GetItemCount() const;
};
CSomeClass::CSomeClass(){
lpData = 0;
}
void CSomeClass::SwapData(CSomeClass &pCopy){
swap(lpData, pCopy.lpData);
}
void CSomeClass::SetItemCount(int count)
{
if( lpData )
lpData->resize(count);
else
if( lpData = new vector<int>() )
SetItemCount(count);
}
size_t CSomeClass::GetItemCount() const{
size_t count = 0;
if( lpData )
count = lpData->size();
return count;
}
void Notify(const CSomeClass &srs,
const CSomeClass &dst, const CSomeClass &cpy, const char * msg = 0);
int main()
{
CSomeClass srs;
CSomeClass dst;
CSomeClass cpy;
Notify(srs, dst, cpy, "INIT : ");
srs.SetItemCount(10);
Notify(srs, dst, cpy, "SRS::SetItemCount(10) : ");
dst.SwapData(srs);
Notify(srs, dst, cpy, "DST::SwapData(SRS) : ");
cpy.SetItemCount(5);
Notify(srs, dst, cpy, "CPY::SetItemCount(10) : ");
cpy.SwapData(dst);
Notify(srs, dst, cpy, "CPY::SwapData(DST) : ");
cin.get();
return 0;
}
void Notify(const CSomeClass &srs, const CSomeClass &dst,const CSomeClass &cpy, const char * msg)
{
if( msg )
cout<<msg<<endl;
cout<<"SRS : "<<srs.GetItemCount()<<endl;
cout<<"DST : "<<dst.GetItemCount()<<endl;
cout<<"CPY : "<<cpy.GetItemCount()<<endl;
}
I2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwogCmNsYXNzIENTb21lQ2xhc3MKewogICAgcHJvdGVjdGVkOgogICAgdmVjdG9yPGludD4gKiBscERhdGE7CiAgICBwdWJsaWM6CiAgICBDU29tZUNsYXNzKCk7Cgl2b2lkIFN3YXBEYXRhKENTb21lQ2xhc3MgJnBDb3B5KTsKICAgIHZvaWQgU2V0SXRlbUNvdW50KGludCBjb3VudD0gMC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