C++程序设计语言书中11章实现Sting部分不会的地方

对这里实现String中作者没写的四个函数

 
// 这两个是成员函数
mString& operator+=( const mString&);
mString& operator +=(const char*);
 
// 这两个是非成员函数
mString operator+( const mString&, const mString&);
mString operator+( const mString&, const char*);
 
的具体实现不是很有想法。
 
问题是:
 
1、 前两个类成员函数返回的是引用,而这里String的具体实现是用的char,根据作者写的代码,自己感觉这里肯定会修改原String的char指针,也就返回的不再是之前的引用对象了。
 
2、后面两个实现中,比如第一个:怎样来调用形参里面String对象的内部实现,即char*
 
还有一个问题是,自己写的时候也是把mString类内的Cref还有Srep写在.cpp中,但链接不通过,最后改成写在声明mString.h中,原作者的代码链接如下
 
已写完代码记录如下
  1 #pragma once
  2 #include <string.h>
  3 #include <iostream>
  4 
  5 class mString
  6 {
  7 
  8 
  9 public:
 10     mString(void);
 11     mString(const char*);
 12     mString(const mString&);
 13     mString& operator=(const char*);
 14     mString& operator=(const mString&);
 15 
 16     ~mString(void);
 17 
 18     class Cref;
 19     class Range{ };
 20 
 21     void check(int i) const { if (i<0 || (rep->sz)<=i) throw Range(); }
 22     char read(int i) const { return rep->s[i]; }
 23     void write(int i, char c) { rep = rep->get_own_copy(); rep->s[i] = c; }
 24     Cref operator[](int i) { check(i); return Cref(*this, i); }
 25     char operator[](int i) const { check(i); return rep->s[i]; }
 26     int size() const { return rep->sz; }
 27 
 28 
 29     mString& operator+=(const mString&);
 30     mString& operator+=(const char*);
 31 
 32     friend std::ostream operator<<(std::ostream&, const mString&);
 33     friend std::istream& operator>>(std::istream&, mString&);
 34     friend bool operator==(const mString& x, const char* s) { return strcmp(x.rep->s, s) == 0; }
 35     friend bool operator==(const mString& x, const mString& y) { return strcmp(x.rep->s, y.rep->s) == 0; }
 36     friend bool operator!=(const mString& x, const char* s) { return strcmp(x.rep->s, s) != 0; }
 37     friend bool operator!=(const mString& x, const mString& y) { return strcmp(x.rep->s, y.rep->s) != 0; }
 38 
 39 
 40 private:
 41     struct Srep; //表示
 42     Srep* rep;
 43 
 44     struct mString::Srep {
 45         char* s; //到元素的指针
 46         int sz; //字符个数
 47         int n; //引用计数
 48 
 49         Srep(int nsz, const char* p)
 50         {
 51             n = 1;
 52             sz = nsz;
 53             s = new char[sz+1];
 54             strcpy_s(s, sz, p);
 55         }
 56 
 57         ~Srep() { delete[] s; }
 58 
 59         Srep* get_own_copy()
 60         {
 61             if (1 == n)
 62             {
 63                 return this;
 64             }
 65             --n;
 66             return new Srep(sz, s);
 67         }
 68 
 69         void assign(int nsz, const char* p)
 70         {
 71             if (sz != nsz)
 72             {
 73                 delete[] s;
 74                 sz = nsz;
 75                 s = new char[sz+1];
 76             }
 77             strcpy_s(s, sz, p);
 78         }
 79 
 80     private:
 81         Srep(const Srep&);
 82         Srep& operator=(const Srep&);
 83 
 84     };
 85 
 86     class mString::Cref {
 87 
 88     public:
 89         operator char() const { return s.read(i); }
 90         void operator=(char c) { s.write(i,c); }
 91 
 92     private:
 93         friend class mString;
 94         Cref(mString& ss, int ii) :s(ss), i(ii) {}
 95 
 96     private:
 97         mString& s;
 98         int i;
 99 
100     };
101 
102     /*class mString::Range {
103     public:
104         Range(void) { std::cout << "go here:Range()" << std::endl;}
105     };*/
106 
107 };
108 
109 mString operator+(const mString&, const mString&);
110 mString operator+(const mString&, const char*);
 1 #include "StdAfx.h"
 2 #include "mString.h"
 3 
 4 mString::mString(void)
 5 {
 6     rep = new Srep(0, "");
 7 }
 8 
 9 mString::mString(const mString& rhs)
10 {
11     (rhs.rep->n)++;
12     rep = rhs.rep;
13 }
14 
15 mString::mString(const char* rhs)
16 {
17     rep = new Srep(strlen(rhs), rhs);
18 }
19 
20 mString::~mString(void)
21 {
22     if (--rep->n == 0)
23     {
24         delete rep;
25     }
26 }
27 
28 mString& mString::operator=(const mString& rhs)
29 {
30     rhs.rep->n++;
31     if ( --rep->n == 0)
32     {
33         delete rep;
34     }
35     rep = rhs.rep;
36     return *this;
37 }
38 
39 mString& mString::operator=(const char* rhs)
40 {
41     if (rep->n == 1)
42     {
43         rep->assign(strlen(rhs), rhs);
44     }
45     else
46     {
47         rep->n--;
48         rep = new Srep(strlen(rhs), rhs);
49     }
50     return *this;
51 }
52 
53 mString& mString::operator+=(const mString&)
54 {
55 
56 }
57 
58 mString& mString::operator+=(const char*)
59 {
60 
61 }
62 
63 mString operator+(const mString& x, const mString& y)
64 {
65     int length = x.size() + y.size() + 1;
66     const char* s = new char[length];
67 //    strcpy_s(s, length, ); //这里不知道怎么来调用x 或者y的内部char*
68     return mString(s);
69 
70 }
71 mString operator+(const mString& )

 
原文地址:https://www.cnblogs.com/ziyoudefeng/p/2457840.html