C++友元学习笔记

友元可以分为三种:

1.友元函数;

2.友元类;

3.友元成员函数;

友元的好处,通过友元函数,可以赋予函数与类成员函数相同的访问权限,友元函数是可以访问类私有成员的非类成员函数。

因为友元函数不是类的成员函数,所以不能用类对象调用成员函数的方式(类成员符)调用友元函数。

友元函数的创建

template<class T>
class matrix
{
	friend ostream& operator<<(ostream& os, const matrix<T>& m)
	{
		
		for (int i = 0; i < m.theRows; i++)
		{
			for (int j = 0; j < m.theColumns; j++)
			{
				os << m.element[i*m.theColumns + j]<<" ";
			}
			os << endl;
		}
		os << "Output matrix finished" << endl;
		return os;
		
	};
	friend istream& operator >> (istream& os, matrix<T>& m)
	{
		size_t number = m.theColumns * m.theRows;
		for (int i = 0; i < number; i++)
		{
			os >> m.element[i];
		}
		cout << "Input fanished" << endl;
		return os;
	};
public:
	matrix(int theRows = 0, int theColumns = 0);
	matrix(const matrix<T>&);
	~matrix() { delete[] element; };
	int rows()const { return theRows; };
	int cols()const { return theColumns; };
	T& operator()(int i, int j)const;
	matrix<T>& operator=(const matrix<T>&);
	matrix<T> operator+()const;
	matrix<T> operator+(const matrix<T>&)const;
	matrix<T> operator-()const;
	matrix<T> operator-(const matrix<T>&)const;
	matrix<T> operator*(const matrix<T>&)const;
	matrix<T>& operator+=(const T&);
	void Input();
	void Output();
	void Transport();
private:
	int theRows;
	int theColumns;
	T* element;
};
friend ostream& operator<<(ostream& os, const matrix<T>& m);
就是创建友元函数的方式;

函数创建了就需要定义

友元函数的定义既不需要类头,也不要关键字friend;

<<是最为常用的友元函数,重载<<运算符

在平时编程中我们经常使用cout,这是一个ostream的对象,它是智能的,可以识别C++的基本类型。

作为友元函数重载<<是为了达到给class 对象 输出带来方便;

今天在友元上掉进了坑里,按照理论上来说,在类外定义友元函数是没毛病的,但是我在如下定义的方式是程序编译不成功,总是报非法使用显式模板

template<class T>
std::ostream& operator<< <T>(ostream& os, const matrix<T>& m)
{

	for (int i = 0; i < m.theRows; i++)
	{
		for (int j = 0; j < m.theColumns; j++)
		{
			os << m.element[i*m.theColumns + j] << " ";
		}
		os << endl;
	}
	os << "Output matrix finished" << endl;
	return os;
};
在网上找了很久也没有解决问题,在摸索中,我在类内直接定义友元函数,然后就顺利运行了程序,至于其中的道理,我还需要再摸索摸索!

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值