各位大哥帮帮我,这道题是讲的什么,看了很长时间还是没看懂
过几天就要交了,小弟刚学C++,英文的题实在理解不好,各位大哥一定帮忙分析下啊,
The following class is the my_string class that has reference semantics for copying. This class has shallow copy semantics because pointer assignment replaces copying. The techniques illustrated are common for this type of aggregate. We use the class str_obj to create object values. The type str_obj is a required implmentation detail for my_string. It could not be directly placed in my_string without destroying the potential many_to_one relationship between objects of type my_string and referenced values of type str_obj. The values of my_string are in the class str_obj, which is an auxiliary class for my_string's use only. The publicly used class my_string handles the str_obj instances and is called a handler class.
class str_obj
{
public:
int ref_cnt;
char* s;
str_obj() : ref_cnt(1), len(0)
{
s = new char[1];
assert(s != 0);
s[0] = 0;
}
str_obj(const char* p) : ref_cnt(1)
{
len = strlen(p);
s=new char[len+1];
assert(s != 0);
strcpy(s, p);
}
~str_obj() { delete [] s; }
private:
int len;
};
class my_string
{
public:
my_string()
{
st = new str_obj;
assert(st != 0);
}
my_string(const char* p)
{
st = new str_obj(p);
assert(st != 0);
}
~my_string()
{
if (--st -> ref_cnt ==0)
delete st;
}
void print() const { cout << st->s; }
};
Implement:
overloaded assignment operator = for my_string;
overloaded subscript operator [] to return the ith character in the my_string. If there is no such character, the value -1 is to be returned.
overloaded the function call operator () to operate substring. The notation is my_string(from, to), where from is the beginning of the substring and to is the end. Use this to search a string for a character sequence and return true if the subsequence is found.