l*********y 发帖数: 142 | 1 #include
#include
#include
#include
#include
#include
#include
using namespace std;
class Counter {
public :
Counter() {
counter = 0;
}
void increment() {
counter++;
}
void decrement() {
counter --;
}
int getValue() {
return counter;
}
private:
int counter;
};
class COWString {
public:
COWString() {
pointer = NULL;
rc = new Counter();
}... 阅读全帖 |
|
s******n 发帖数: 3946 | 2 觉得那个不太好,用share pointer:
class COWString {
share_ptr data;
public:
COWString(const char* input)
{
data = share_ptr(new CString(input));
}
COWString(const COWString& str)
{
data = str.data;
}
COWString& operator=(const COWString& str)
{
data = str.data;
return *this;
}
COWString& SetAt(int index, char c)
{
CString* newStr = new CString(data->c_str());
newStr->SetAt(index, c);
data = share_ptr(newStr);
return *this;
}
char GetAt(int index) const
{
... 阅读全帖 |
|
s******n 发帖数: 3946 | 3 觉得那个不太好,用share pointer:
class COWString {
share_ptr data;
public:
COWString(const char* input)
{
data = share_ptr(new CString(input));
}
COWString(const COWString& str)
{
data = str.data;
}
COWString& operator=(const COWString& str)
{
data = str.data;
return *this;
}
COWString& SetAt(int index, char c)
{
CString* newStr = new CString(data->c_str());
newStr->SetAt(index, c);
data = share_ptr(newStr);
return *this;
}
char GetAt(int index) const
{
... 阅读全帖 |
|
s******n 发帖数: 3946 | 4 就是写两个overload吧?
COWString& operator=(const char*)
COWString& operator=(const COWString&) |
|
s******n 发帖数: 3946 | 5 就是写两个overload吧?
COWString& operator=(const char*)
COWString& operator=(const COWString&) |
|