当前位置: 首页 > article >正文

c++介绍运算符重载九

这段代码中我们可以看到同样是运算符,它的作用是不同的a=a<<3;是c++内置的移位运算符。

cout<<"hello world"中<<它的作用是插入运算符。将字符串传递给cout对象,从而将字符输出到终端上。这种功能是如何实现的?其实就是将运算符作为一个函数,对函数进行重载。

#include<iostream>
#include<vector>
using namespace std;
int main()
{
	vector<int>numbers{ 1,2,3,5,7,9 };
	cout << numbers << endl;
	int a = 3;
	a = a << 3;
}

编译错误
error C2679: 二元“<<”: 没有找到接受“std::vector<int,std::allocator<int>>”类型的右操作数的运算符(或没有可接受的转换)

其实就是找不到对应参数类型的重载函数。为了让上述代码工作,我们需要重写一个重载函数,用来实现整形向量传递给字符串输出对象。

下面实现对整形向量的重载代码

#include<iostream>
#include<vector>
using namespace std;

ostream& operator<<(ostream& o, const vector<int>& numbers)
{
	o << "[";
	unsigned int last = numbers.size();
	for (int i = 0; i < last; i++)
	{
		o << numbers[i];
		if (i != last - 1)
			o << ",";
	}
	o << "]";
	return o;
}
int main()
{
	vector<int>numbers{ 1,2,3,5,7,9 };
	cout << numbers << endl;
	int a = 3;
	a = a << 3;
}

打印结果 

 

上面是类外非成员函数进行重载。也可以对类内成员函数进行重载。

下面是同样两个类型的对象相加,并输出结果

#include<iostream>
#include<vector>
using namespace std;

//ostream& operator<<(ostream& o, const vector<int>& numbers)
//{
//	o << "[";
//	unsigned int last = numbers.size();
//	for (int i = 0; i < last; i++)
//	{
//		o << numbers[i];
//		if (i != last - 1)
//			o << ",";
//	}
//	o << "]";
//	return o;
//}
//int main()
//{
//	vector<int>numbers{ 1,2,3,5,7,9 };
//	cout << numbers << endl;
//	int a = 3;
//	a = a << 3;
//}

class Complex {
private:
	float r;
	float i;
public:
	Complex(float real, float imaginary) :r(real), i(imaginary) {};
	Complex operator+(const Complex& other)const 
	{
		return Complex(this->r + other.r, this->i+other.i);
	}
    friend ostream& operator<<(ostream &o, const Complex& a)
	{
		o <<"r:"<<a.r<<" "<<"i:"<<a.i;
		return o;
	}
};

int main()
{
	Complex a(0.1, 2);
	Complex b(0.4,-2.1);
	Complex c = a + b;
	cout << c;
	return 0;
}

 自增运算符重载过程

class ExampleClass
{
private:
	int m_a;
	//前缀增量运算符
	ExampleClass& operator++()
	{
		m_a++;
		return *this;
	}
	//后缀增量运算符
	ExampleClass operator++(int)
	{
		ExampleClass temp = *this;
		operator++();
		return temp;
	}
};

 函数调用运算符,我们定义一个一次函数表达式

struct LinearFuction {
	double k;
	double b;
	double operator()(double x)const {
		return k * x + b;
	}

};
int main()
{
	LinearFuction f{ 1.5,3 };
	cout << "f(0)=" << f(0) << endl;
	cout << "f(3)=" << f(3) << endl;
}

间接引用运算符(->),它的返回值必须是一个指针或者一个对象或者对象的引用,并且这个对象的间接引用也被重载了。

struct Complex {

	float r, i;
};
class LocalPtr {
private:
	Complex* m_ptr;
public:
	LocalPtr(Complex* p) :m_ptr(p) {}
	Complex* operator->() { return m_ptr; }
	~LocalPtr() {
		if (m_ptr)
			delete m_ptr;
	}
	LocalPtr(const LocalPtr&) = delete;
	LocalPtr& operator=(const LocalPtr&) = delete;
};
int main()
{
	LocalPtr pComplex1(new Complex{ 0.1,0.5 });
	pComplex1->i++;
}

 我们常用的智能指针就是重载间接引用运算符来实现的

我们定义一个Complex结构,用于表示复数。接着我们定义一个名叫LocalPtr的类,它是一个简单的指针包装类,它定义了一个指向Complex类的指针。重载了一个间接引用运算符(->),它返回指向Complex结构的指针。在主函数中可以直接访问m_ptr所指向的结构体中成员。类对象销毁时调用析构函数。释放m_ptr缩指向的内存 。为了防止多个对象m_ptr成员指向同一地址。并在析构函数中释放同一块内存。localPtr删除了拷贝构造函数和赋值运算符。

下彪运算符,重载后可以让对象向数组一样访问元素。

代码如下

class Array 
{
private:
	vector<float>number;
public:
	Array(vector<float>n) :number(n) {};
	float operator[](int i)
	{
		int size = number.size();
		if (i < 0)
			return number[size + i];

		return number[i];
	}
};
int main()
{
	Array arr(vector<float>{1,2,3,4,5,6,7,8,9});
	cout << arr[1]<<endl;
	cout << arr[-1] << endl;
}

http://www.kler.cn/a/582559.html

相关文章:

  • vscode接入DeepSeek 免费送2000 万 Tokens 解决DeepSeek无法充值问题
  • 5秒学会excel中序号列自动增加,不是拖动,图解加说明,解决序号自增多了手拖太累
  • VSTO(C#)Excel开发5:调整表格到一页
  • 【ELK】ElasticSearch 集群常用管理API操作
  • ChebyKAN0、ChebyKAN1 网络阅读
  • 常用Kotlin方法
  • jmeter接口测试(三)
  • 前端 - uniapp - - 滚动容器scroll-view实现横向滚动
  • BFS最短路径(十六)127. 单词接龙 困难
  • zabbix报警结合AI进行智能分析
  • 某快餐店用户市场数据挖掘与可视化
  • c++ enum使用笔记
  • RocketMQ 集群架构与部署实践(一)
  • Flutter_学习记录_device_info_plus 插件获取设备信息
  • Java糊涂包(Hutool)的安装教程并进行网络爬虫
  • FreeBSD下安装npm Node.js的22版本 并简单测试js服务器
  • 【Golang】第三弹----运算符
  • Python多版本环境管理UV
  • Linux上位机开发实战(qt编译之谜)
  • Spring 框架面试题集:常见问题解析