【ArcGIS Pro二次开发】(78):批量合并GDB数据库
《C++新经典设计模式》之附录B 引用技术基础理论和实践
引用技术.cpp
#include <iostream>
#include <memory>
#include <string>
#include <cstring>
using namespace std;
namespace B_1_1
{
void func()
{
shared_ptr<int> myp(new int(5));
int icount = myp.use_count();
cout << "icount = " << icount << endl;
{
shared_ptr<int> myp2(myp);
icount = myp2.use_count();
cout << "icount = " << icount << endl;
}
icount = myp.use_count();
cout << "icount = " << icount << endl;
}
}
namespace B_1_2
{
void func()
{
string str1 = "I Love China!";
string str2 = str1;
cout << "str1 address: " << static_cast<const void *>(str1.c_str()) << endl;
cout << "str2 address: " << static_cast<const void *>(str2.c_str()) << endl;
}
}
namespace B_2_1
{
class mystring
{
char *point;
public:
mystring(const char *tmpstr = "")
{
point = new char[strlen(tmpstr) + 1];
strcpy(point, tmpstr);
cout << "1" << endl;
}
~mystring()
{
delete[] point;
}
mystring(const mystring &tmpstr)
{
point = new char[strlen(tmpstr.point) + 1];
strcpy(point, tmpstr.point);
cout << "2" << endl;
}
mystring &operator=(const mystring &tmpstr)
{
if (this == &tmpstr)
return *this;
delete[] point;
point = new char[strlen(tmpstr.point) + 1];
strcpy(point, tmpstr.point);
cout << "3" << endl;
return *this;
}
};
}
namespace B_2_2345
{
class mystring
{
struct stringvalue
{
size_t refcount;
char *point;
stringvalue(const char *tmpstr = "") : refcount(1)
{
point = new char[strlen(tmpstr) + 1];
strcpy(point, tmpstr);
}
~stringvalue()
{
delete[] point;
}
};
stringvalue *pvalue;
public:
mystring(const char *tmpstr = "") : pvalue(new stringvalue(tmpstr)) {}
mystring(const mystring &tmpstr) : pvalue(tmpstr.pvalue)
{
++pvalue->refcount;
}
mystring &operator=(const mystring &tmpstr)
{
if (this == &tmpstr)
return *this;
--pvalue->refcount;
if (pvalue->refcount == 0)
delete pvalue;
pvalue = tmpstr.pvalue;
++pvalue->refcount;
return *this;
}
~mystring()
{
--pvalue->refcount;
if (pvalue->refcount == 0)
delete pvalue;
}
};
}
namespace B_2_6
{
class mystring
{
struct stringvalue
{
size_t refcount;
char *point;
stringvalue(const char *tmpstr = "") : refcount(1)
{
point = new char[strlen(tmpstr) + 1];
strcpy(point, tmpstr);
}
~stringvalue()
{
delete[] point;
}
};
stringvalue *pvalue;
public:
mystring(const char *tmpstr = "") : pvalue(new stringvalue(tmpstr)) {}
mystring(const mystring &tmpstr) : pvalue(tmpstr.pvalue)
{
++pvalue->refcount;
}
mystring &operator=(const mystring &tmpstr)
{
if (this == &tmpstr)
return *this;
--pvalue->refcount;
if (pvalue->refcount == 0)
delete pvalue;
pvalue = tmpstr.pvalue;
++pvalue->refcount;
return *this;
}
~mystring()
{
--pvalue->refcount;
if (pvalue->refcount == 0)
delete pvalue;
}
public:
char &operator[](int idx)
{
if (pvalue->refcount > 1)
{
--pvalue->refcount;
pvalue = new stringvalue(pvalue->point);
}
return pvalue->point[idx];
}
};
}
namespace B_2_7
{
class mystring
{
struct stringvalue
{
size_t refcount;
char *point;
bool shareable;
stringvalue(const char *tmpstr = "") : refcount(1), shareable(true)
{
point = new char[strlen(tmpstr) + 1];
strcpy(point, tmpstr);
}
~stringvalue()
{
delete[] point;
}
};
stringvalue *pvalue;
public:
mystring(const char *tmpstr = "") : pvalue(new stringvalue(tmpstr)) {}
mystring(const mystring &tmpstr)
{
if (tmpstr.pvalue->shareable)
{
pvalue = tmpstr.pvalue;
++pvalue->refcount;
}
else
{
pvalue = new stringvalue(tmpstr.pvalue->point);
}
}
mystring &operator=(const mystring &tmpstr)
{
if (this == &tmpstr)
return *this;
--pvalue->refcount;
if (pvalue->refcount == 0)
delete pvalue;
pvalue = tmpstr.pvalue;
++pvalue->refcount;
return *this;
}
~mystring()
{
--pvalue->refcount;
if (pvalue->refcount == 0)
delete pvalue;
}
public:
char &operator[](int idx)
{
if (pvalue->refcount > 1)
{
--pvalue->refcount;
pvalue = new stringvalue(pvalue->point);
}
pvalue->shareable = false;
return pvalue->point[idx];
}
};
}
int main()
{
#if 0
B_1_1::func();
#endif
#if 0
B_1_2::func();
#endif
#if 0
using namespace B_2_1;
mystring kxstr1("I Love China!");
mystring kxstr2 = "I Love China!";
mystring kxstr3 = kxstr1;
kxstr2 = kxstr1;
#endif
#if 0
using namespace B_2_2345;
mystring kxstr1("I Love China!");
mystring kxstr2("I Love China!");
mystring kxstr3 = kxstr1;
#endif
#if 0
using namespace B_2_6;
mystring ms = "I Love China!";
cout << ms[0] << endl;
ms[0] = 'Y';
cout << ms[0] << endl;
#endif
#if 1
using namespace B_2_7;
mystring kxstr4 = "I Love China!";
char *mypoint = &kxstr4[0];
mystring kxstr5 = kxstr4;
*mypoint = 'Y';
cout << kxstr4[0] << endl;
cout << kxstr5[0] << endl;
#endif
cout << "Over!\n";
return 0;
}