大白菜豆腐 2023-04-09 22:45 采纳率: 100%
浏览 18
已结题

关于#c++#的问题:关于多种运算符重载的问题

img


关于多种运算符重载的问题,最好多几条注释,解释清楚一点,注意其返回值的不同

  • 写回答

4条回答 默认 最新

  • 关注

    代码:

    
    #include <iostream>
    using namespace std;
    class CStr
    {
    private:
        char* data;
        int len;
    public:
        CStr(const char* d = 0)
        {
            //cout << "construct----------" << endl;
            if (d == 0)
            {
                data = 0;
                len = 0;
            }
            else
            {
                len = 0;
                while (d[len] != '\0')
                    len++;
    
                data = new char[len + 1];//多申请一个字节给\0
                for (int i = 0; i < len; i++)
                    data[i] = d[i];
                data[len] = '\0'; //结束符
            }
            
            
        }
        ~CStr() 
        {
            //cout << "disconstruct....." << endl;
            //析构函数中不要delete data;否则+运算回得不到结果
        }
    
        CStr& operator = (CStr& s)
        {
            //cout << "=:" << endl;
            delete[] data;
            len = s.len;
            data = new char[len + 1];
            for (int i = 0; i < len; i++)
                data[i] = s.data[i];
            data[len] = '\0';
            return *this;
        }
    
        CStr operator +(const CStr& a)
        {
            //cout << "+" << endl;
            char* p = new char[len + a.len + 1];
            int i = 0;
            for (; i < len; i++)
                p[i] = data[i];
            for (int j = 0; j < a.len; j++)
                p[i + j] = a.data[j];
            p[len + a.len] = '\0';
            CStr t(p);
            delete[] p;
            return t;
        }
    
        CStr operator +=(const CStr& a)
        {
            this->data = (char*)realloc(this->data, len + a.len + 1); //扩容
            for (int j = 0; j < a.len; j++)
                this->data[len + j] = a.data[j];
            this->data[len + a.len] = '\0';
            len += a.len; //修正长度
            return *this;
        }
    
        bool operator >(const CStr& a)
        {
            int t = len > a.len ? a.len : len;
            for (int i = 0; i < t; i++)
            {
                if (data[i] > a.data[i])
                    return true;
            }
            return false;
        }
        bool operator <(const CStr& a)
        {
            int t = len > a.len ? a.len : len;
            for (int i = 0; i < t; i++)
            {
                if (data[i] < a.data[i])
                    return true;
            }
            return false;
        }
        bool operator ==(const CStr& a)
        {
            if (len != a.len)
                return false;
            
            for (int i = 0; i < len; i++)
            {
                if (data[i] != a.data[i])
                    return false;
            }
            return true;
        }
    
        char operator [](int index)
        {
            if (index < len)
                return data[index];
            else
                return '\0';
        }
    
        friend ostream& operator <<(ostream& out, const CStr& a)
        {
            out << a.data;
            return out;
        }
    
    };
    
    int main()
    {
        char buf[100] = { 0 };
        cout << "请输入一个字符串创建CStr 实例a:" << endl;
        cin >> buf;
        CStr a(buf);
    
        cout << "请输入一个字符串创建CStr 实例b:" << endl;
        cin >> buf;
        CStr b(buf);
    
        CStr c;
        c = a;
        cout << "CStr c=a; c:" << c << endl;
    
        CStr d = a + b;
    
        cout << "CStr d=a+b; d:" << d << endl;
    
        cout << "a>b:" << (a > b) << endl;
    
        cout << "请输入要获取的实例a的元素下标:";
        int index;
        cin >> index;
        cout << "a[" << index << "]=" << a[index] << endl;
    
    
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 4月25日
  • 已采纳回答 4月17日
  • 创建了问题 4月9日

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。