数组的实现(重载[]、=、==、!=运算符重载)

.h文件

#ifndef PCH_H
#define PCH_H

// TODO: 添加要在此处预编译的标头
#include<iostream>
using namespace std;

class myArray
{
public:
    myArray(int length);
    myArray(const myArray& objs);
    ~myArray();

    void setData(int index, int value);
    int getData(int index);
    int length();
    int& operator[](int i);
    myArray& operator=(myArray &a1);
    bool operator==(myArray &a1);
    bool operator!=(myArray &a1);
protected:
    int m_length;
    int *m_space;

};

#endif //PCH_H

.cpp文件

// pch.cpp: 与预编译标头对应的源文件;编译成功所必需的

#include "pch.h"

// 一般情况下,忽略此文件,但如果你使用的是预编译标头,请保留它。
myArray::myArray(int length)
{
    if (length < 0) {
        length = 0;
    }

    m_length = length;
    m_space = new int[m_length];
}

//拷贝构造 myArray a2 = a1 
myArray::myArray(const myArray& obj)
{
    this->m_length = obj.m_length;
    this->m_space = new int[this->m_length];

    for (int i = 0; i < m_length; i++) {
        this->m_space[i] = obj.m_space[i];
    }
}
myArray::~myArray()
{
    if (m_space != NULL) {
        delete m_space;
        m_space = NULL;
        m_length = -1;
    }
}

void myArray::setData(int index, int value)
{
    m_space[index] = value;
}
int myArray::getData(int index)
{
    return m_space[index];
}
int myArray::length()
{
    return m_length;
}

int& myArray::operator[](int i)//同时当左右值
{
    return m_space[i];
}

//a3 = a1;
myArray& myArray::operator=(myArray &a1)
{
    if (m_space != NULL) {
        delete []m_space;
        //this->m_space = NULL;
        m_length = 0;
    }

    m_length = a1.m_length;
    m_space = new int[m_length];

    for (int i = 0; i < m_length; i++) {
        m_space[i] = a1[i];//m_space[i] = a1.m_space[i];
    }
    return *this;
}

bool myArray::operator==(myArray &a1)
{
    if (m_length != a1.m_length) {
        return false;
    }
    for (int i = 0; i < m_length; i++) {
        if (m_space[i] != a1[i]) {
            return false;
        }
    }
    return true;
}

bool myArray::operator!=(myArray &a1) {
    /*if (m_length != a1.m_length) {
        return true;
    }
    for (int i = 0; i < m_length; i++) {
        if (m_space[i] == a1[i]) {
            return false;
        }
    }
    return true;*/
    if (*this == a1) {
        return false;
    }
    else {
        return true;
    }
}

业务应用main.c

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

int main()
{
    myArray a1(10);
    for (int i = 0; i < a1.length(); i++) {
        a1.setData(i, i);
        a1[i] = i;//a1.operator[i] 函数返回值当左值,要返回引用  //int& operator[](int i)
    }

    cout << "
打印数据a2:" << endl;
    myArray a2 = a1;
    for (int i = 0; i < a2.length(); i++) {
        //cout << a2.getData(i) << " ";
        cout << a2[i] << " ";//int operator[](int i)
    }
    cout << endl;

    myArray a3(5);
    a3 = a1;//a3.operator=(a1)//myArray& operator=(myArray &a1)
    a3 = a2 = a1;

    cout << "
打印数据a3:" << endl;
    for (int i = 0; i < a3.length(); i++) {
        //cout << a2.getData(i) << " ";
        cout << a3[i] << " ";//int operator[](int i)
    }
    cout << endl;
    
    myArray a4(5);
    if (a3 == a1) {//a3.operator==(a1) //bool operator==(myArray &a1)
        cout << "相等" << endl;
    }
    
    if (a4 != a1) {//a4.operator!=(a1) //bool operator!=(myArray &a1)
        cout << "不相等" << endl;
    }
    cout << endl;
    std::cout << "Hello World!
"; 
}
原文地址:https://www.cnblogs.com/jly594761082/p/10584124.html