C++ =、[ ]、==、!=的重载

该博客主要围绕C++中=、[]、==、!=运算符的重载展开。运算符重载是C++的重要特性,能让自定义类型使用这些运算符,增强代码可读性与可维护性,在信息技术领域的C++编程中应用广泛。
//Array.h
#pragma once

#include<iostream>
using namespace std;

class Array
{
public:
	Array(int length);
	Array(const Array& obj);
	~Array();
public:
	void setdata(int index, int data);
	int getdata(int index);
	int getlength();
	int& operator[](int i);
	Array& operator=(Array &t1);
	bool operator==(Array &t);
	bool operator!=(Array &t);
private:
	int m_lentgh;
	int *m_space;
};
//array.cpp
#include"Array.h"
	
Array::Array(int length)//有参构造函数
{
	if (length < 0)
	{
		length = 0;
		m_space = new int[m_lentgh];
	}
	m_lentgh = length;
	m_space = new int[m_lentgh];
}
Array::Array(const Array& obj)//拷贝构造函数
{
	this->m_lentgh = obj.m_lentgh;
	this->m_space = new int[m_lentgh];
	for (int i = 0; i < m_lentgh; i++)
	{
		this->m_space[i] = obj.m_space[i];
	}
}
Array::~Array()//析构函数
{
	if (m_space != NULL)
	{
		delete[] m_space;
		m_space = NULL;
	}
}
//t1.setdata(i,i)
void Array::setdata(int index, int data)
{
	m_space[index] = data;
}
int Array::getdata(int index)
{
	return m_space[index];
}
int Array::getlength()
{
	return m_lentgh;
}
int& Array::operator[](int i)
{
	return m_space[i];
}
Array& Array::operator=(Array &t1)//=的重载
{
	if (this->m_space != NULL)
	{
		delete[] this->m_space;
	}
	this->m_lentgh = t1.m_lentgh;
	this->m_space = new int[m_lentgh];
	for (int i = 0; i < m_lentgh; i++)
	{
		this->m_space[i] = t1.m_space[i];
	}
	return *this;
}
bool Array::operator==(Array &t)//==的重载
{
	if (this->m_lentgh != t.m_lentgh)
	{
		return false;
	}
	for (int i = 0; i < this->m_lentgh; i++)
	{
		if (this->m_space[i] != t.m_space[i])
		{
			return false;
		}
	}
	return true;
}
bool Array::operator!=(Array &t)//!=的重载
{
	if (this->m_lentgh != t.m_lentgh)
	{
		return true;
	}
	for (int i = 0; i < t.m_lentgh; i++)
	{
		if (m_space[i] != t.m_space[i])
		{
			return true;
		}
	}
	return false;
}
//main.h
#include<iostream>
#include"Array.h"
using namespace std;

int main()
{
	Array t1(10);
	for (int i = 0; i < t1.getlength(); i++)
	{
		t1[i] = i;
	}
	/*for (int i = 0; i < t1.getlength(); i++)
	{
		cout << "t1="<<t1.getdata(i)<< endl;
	}*/

	Array t2(t1);
	for (int i = 0; i < t2.getlength(); i++)
	{
		cout << t2[i]<< endl;
	}

	Array t3(5);
	Array t4(10);
	t3 = t1;
	t3 = t4 = t1;
	for (int i = 0; i < t3.getlength(); i++)
	{
		cout << t3[i]<<" ";
	}

	Array t5(5);
	for (int i = 0; i < t5.getlength(); i++)
	{
		t5[i] = i;
	}
	Array t6(t5);
	if (t5 == t6)
	{
		cout << "相等"<< endl;
	}


	if (t3 != t6)
	{
		cout << "不相等"<< endl;
	}
	for (int i = 0; i < t5.getlength(); i++)
	{
		cout << t5[i] << " ";
	}
	
	system("pause");
}

 

### C++中不等于运算符!=)的重载方法及示例 在C++中,不等于运算符(`!=`)可以通过重载实现自定义类型的比较功能。以下是一个详细的讲解和代码示例。 #### 不等于运算符重载的基本原理 不等于运算符(`!=`)通常与等于运算符(`==`)一起重载,因为两者的逻辑通常是互补的。通过重载`!=`运算符,可以定义两个对象是否不相等的规则[^3]。 #### 示例代码 以下是一个完整的代码示例,展示如何重载`!=`运算符: ```cpp #include <iostream> #include <string> using namespace std; // 定义一个Person类 class Person { public: // 构造函数 Person(string name, int age) { m_name = name; m_age = age; } // 重载"=="运算符 bool operator==(const Person &p) const { if (p.m_name == this->m_name && p.m_age == this->m_age) { return true; } else { return false; } } // 重载"!="运算符 bool operator!=(const Person &p) const { return !(*this == p); // 利用已有的"=="运算符实现 } string m_name; // 姓名 int m_age; // 年龄 }; // 测试函数 void test01() { Person p1("Tom", 18); Person p2("James", 19); if (p1 != p2) { cout << "p1与p2不相等" << endl; } else { cout << "p1与p2相等" << endl; } } int main() { test01(); return 0; } ``` #### 输出结果 运行上述代码后,输出如下: ``` p1与p2不相等 ``` #### 代码解析 1. 在`Person`类中,`!=`运算符被定义为成员函数,并接受一个`Person`类型的常量引用作为参数。 2. `!=`运算符的实现依赖于`==`运算符的结果。通过调用`*this == p`,判断两个对象是否相等,然后取反得到最终结果。 3. 在测试函数`test01`中,创建了两个`Person`对象`p1`和`p2`,并使用`!=`运算符进行比较。 #### 注意事项 - 确保`!=`运算符的逻辑与`==`运算符一致,避免出现矛盾的情况。 - 如果类中已经定义了`==`运算符,可以通过复用其逻辑来简化`!=`运算符的实现。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值