首页 > 代码库 > 重载加法运算符

重载加法运算符

#include<stdio.h>
#include<string.h>

class String{
        public:
                String(const char *str = NULL);
                char *m_data;
                char *addstr;
                String &operator + (const String *rhs);
};

String :: String(const char *str){      //定义构造函数
        if(str == NULL){
                m_data = http://www.mamicode.com/new char[1];
                m_data[0] = ‘\0‘;
        }else{
                m_data = http://www.mamicode.com/new char[strlen(str)+1];
                strcpy(m_data,str);
        }
}

String &String :: operator+(const String &rhs){         //重载加法运算
        addstr = new char[strlen(m_data)+strlen(rhs.m_data)+1;
        strcpy(addstr,m_data);
}技术分享

重载加法运算符