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

Posted jly594761082

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数组的实现(重载[]===!=运算符重载)相关的知识,希望对你有一定的参考价值。

.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!
"; 
}

 

以上是关于数组的实现(重载[]===!=运算符重载)的主要内容,如果未能解决你的问题,请参考以下文章

C++ 运算符重载

如何使用运算符重载来简化两个分数的添加?

Javascript实现运算符重载

C++提高:运算符重载

C++提高:运算符重载

模板化动态数组中的运算符重载 []