C++类中的拷贝构造、运算符重载实现

最近学习了C++类的拷贝构造函数与运算符重载,据此模仿写了个string类的实现,完成了字符串的部分功能,实现了拷贝构造函数和运算符重载的函数。本文仅作为练习的记录,不讲理论。直接上代码

string.h

//
// Created by xiangqian on 18-3-3.
//

#ifndef STRING_STRING_H
#define STRING_STRING_H

#include <stdio.h>

namespace xq {

    class String {
    public:
        String(const char * = NULL);

        ~String();

        String(const String &);

        // String a; a = b;
        String& operator=(const String &);
        // String a; a = "hello";
        String& operator=(const char *);

        String& operator+=(const String &);

        String operator+(const String &) const ;

        String& operator+=(const char *);

        String operator+(const char *) const;

        inline const char *data() const {
            return m_data;
        }

    private:
        char *m_data;

    };

}

#endif //STRING_STRING_H

string.cpp

//
// Created by xiangqian on 18-3-3.
//
#include "String.h"
#include <string.h>
#include <iostream>

using namespace xq;
using namespace std;

String::String(const char *str) {
    cout << "constructor" << endl;
    if (NULL == str) {
        m_data = new char[1];
        *m_data = '\0';
    } else {
        int len = strlen(str);
        m_data = new char[len + 1];
        strcpy(m_data, str);
    }
}

String::~String() {
    delete[] m_data;
}

String::String(const String &other) {
    cout << "copy constructor" << endl;
    int len = strlen(other.m_data);
    m_data = new char[len + 1];
    strcpy(m_data, other.m_data);
}

String &String::operator=(const String &other) {

    cout << "operator=(const String &)" << endl;

    if (this == &other)
        return *this;

    delete[] m_data;
    int len = strlen(other.m_data);
    m_data = new char[len + 1];
    strcpy(m_data, other.m_data);
    return *this;
}

String &String::operator=(const char *str) {

    cout << "operator=(const char *)" << endl;

    delete[] m_data;
    if (NULL == str) {
        m_data = new char[1];
        *m_data = '\0';
    } else {
        int len = strlen(str);
        m_data = new char[len + 1];
        strcpy(m_data, str);
    }
    return *this;
}

String &String::operator+=(const String &other) {

    cout << "operator+=(const String &)" << endl;

    int len = strlen(m_data) + strlen(other.m_data);
    char *temp = m_data;
    m_data = new char[len + 1];
    strcpy(m_data, temp);
    strcat(m_data, other.m_data);
    delete[] temp;

    return *this;
}

String String::operator+(const String &other) const {

    cout << "operator+(const String &)" << endl;

    String result;
    result = *this;
    result += other;

    return result;
}

String& String::operator+=(const char * str) {

    cout << "operator+=(const char *)" << endl;

    strcat(m_data,str);

    return *this;
}

String String::operator+(const char * str) const {

    cout << "operator+(const char *)" << endl;

    strcat(m_data,str);

    return *this;
}

测试代码 main.cpp

#include <iostream>
#include "String.h"

using namespace std;
using namespace xq;

int main() {

    String a("Hello");
    String b = a;
    String c;
    String d;
    c = a;
    c = "World!";

    cout << d.data() << endl;
    d += a;
    cout << d.data() << endl;
    d += "World!";
    cout << d.data() << endl;
    cout << "=========================" << endl;
    cout << (a+c).data() << endl;
    cout << (a+"World!").data() << endl;

    return 0;
}

最终输出为:

constructor
copy constructor
constructor
constructor
operator=(const String &)
operator=(const char *)


operator+=(const String &)
Hello
operator+=(const char *)
HelloWorld!
=========================
operator+(const String &)
constructor
operator=(const String &)
operator+=(const String &)
HelloWorld!
operator+(const char *)
copy constructor
HelloWorld!



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值