掘根宝典之C++友元函数与运算符重载

我们在前面学习了重载运算符,赋予C++运算符多种含义。

但是我们还没有更深入的理解重载运算符和友元函数之间的爱恨情仇

我们先看作为类成员的重载运算符的例子

#include<iostream>
using namespace std;
class AA
{
private:
	int a_;
public:
	AA(int a)
	{
		a_ = a;
	}
	int operator+(int x)
	{
		return a_ + x;
	}
};
int main()
{
	AA a = { 2 };
	int b = 8;
	cout << a + b << endl;//结果是10
	cout << b + a << endl;//这是不行的
}

我们发现把左操作数和右操作数换个位置就发现不行了

这是为什么呢?

因为调用重载运算符的规则是左操作数是调用对象

也就是说上面这个例子里,a+b会被展开为a.operator+(b);

而b+a无法被展开

那我们怎么做呢?

有人说,我们再像下面再定义一个的不就好了吗

class AA
{
private:
	int a_;
public:
	AA(int a)
	{
		a_ = a;
	}
	int operator+(int x)
	{
		return a_ + x;
	}
	int operator+(int x, AA t)//编译器不允许的
	{
		return x+t.a_
	}
};

但是问题来了,这是编译器不允许的

那我们到底怎么解决这个问题呢?答案是用友元函数

要为类重载运算符,并将非类的项作为其第一个操作数,则可以用友元函数来反转操作数的顺序

#include<iostream>
using namespace std;
class AA
{
private:
	int a_;
public:
	AA(int a)
	{
		a_ = a;
	}
	int operator+(int x)
	{
		return a_ + x;
	}
	friend int operator+(int x, AA t);//告诉编译器,这个函数是AA类的朋友,可以访问AA类的私有数据
};
int operator+(int x, AA t)
{
	return x + t.a_;
}

int main()
{
	AA a = { 2 };
	int b = 8; 
	AA c = { 10 };
	cout << a + b << endl;
	cout << b + a << endl;
	cout << b + a + c << endl;
	cout << a + b + c<< endl;
}

这下子我们就解决这个问题啦,我们甚至发现居然可以连加

这是因为C++从左至右读取语句

a+b+c被展开为a.operator+(b+c)

再被展开为a.operator+(operator+(b,c));

相关推荐

  1. C++函数运算符重载

    2024-02-13 12:18:01       45 阅读
  2. C++运算符重载

    2024-02-13 12:18:01       53 阅读
  3. C++类模板大全

    2024-02-13 12:18:01       29 阅读
  4. c++标识符,命名

    2024-02-13 12:18:01       46 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-13 12:18:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-13 12:18:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-02-13 12:18:01       82 阅读
  4. Python语言-面向对象

    2024-02-13 12:18:01       91 阅读

热门阅读

  1. 23种设计模式之抽象工厂模式

    2024-02-13 12:18:01       52 阅读
  2. 2月8号作业

    2024-02-13 12:18:01       47 阅读
  3. 14.4 OpenGL图元装配和光栅化:点

    2024-02-13 12:18:01       45 阅读
  4. C# 【WPF】之 INotifyPropertyChanged的简单封装

    2024-02-13 12:18:01       67 阅读
  5. Android录音功能的实现及踩坑记录

    2024-02-13 12:18:01       50 阅读
  6. [日常使用] Shell常用命令

    2024-02-13 12:18:01       46 阅读