C++ 学习笔记3

 

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.

 

#include <iostream>
#include<iomanip>

using namespace std;


// Reference counted my_strings

class str_obj
{
public:
   friend class my_string; // my_string access members
   str_obj() : len(0), ref_cnt(1)
      {
    s = new char[1];
    s[0] = 0;
   }
  
   str_obj(const char* p) : ref_cnt(1)
      {
    len = strlen(p);
    s = new char[len + 1];
        strcpy(s, p);
   }
  
   ~str_obj()
   {
    delete []s;
   }

private:
   int    len, ref_cnt;
   char*  s;
};

class my_string
{

public:
   my_string()
   {
    st = new str_obj;
   }
  
   my_string(const char* p)
   {
    st = new str_obj(p);
   }
  
   my_string(const my_string& str)
   {
    st = str.st;
    st -> ref_cnt++;
   }
  
   ~my_string();
  
   void  assign(const my_string& str);
  
   void  print() const
   {
    cout << st -> s;
   }
  
   my_string& operator=(const my_string& str);
  
   char& operator[](int position);

   char& operator()(int from,int to);

private:
   str_obj*  st;
};

my_string::~my_string()
{
   if (--st -> ref_cnt == 0)
      delete st;
}

void my_string::assign(const my_string& str)
{
   if (str.st != st)
   {
      if (--st -> ref_cnt == 0)
         delete st;
      st = str.st;
      st -> ref_cnt++;
   }
}

my_string& my_string::operator=(const my_string& str)
{
   if (str.st != st)
   {
      if (--st -> ref_cnt == 0)
         delete st;
     
   st = str.st;
      st -> ref_cnt++;
   }
   return *this;
}

 

char& my_string::operator[](int position)
{
   char* s = st -> s;
   for (int i = 0; i != position; ++i)
   {
      if (*s == 0)
         break;
      s++;
   }
   return *s;
}

char& my_string::operator()(int from,int to)
{
 char* s = st -> s;
 for (int i = from-1; i < to; ++i)
 {
    cout<<s[i]<<setw(3);
 }
 return *(s-1);
}

int main()
{
  
   my_string  a, b("11110702 "), c("c++ PROJECT 3 "), d, e ("FANZHAOXING");
  
   a = b;
   //a is now "do "
   d = b = c;
   //these are "do not "
   c = "nothing is here ";
   //invoke conversion constructor
   d.assign(c);
   a.print();
   cout<<endl;
   b.print();
   cout<<endl;
   c.print();
   cout<<endl;
   d.print();
   cout<<endl;
   cout << e[4] << endl;
   cout<<e(2,5)<<endl;

   return 0;
 }

 


 

你可能感兴趣的:(C++ 学习笔记3)