C++ 學習筆記3

來源:互聯網
上載者:User

 

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;
 }

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.