C++友元学习笔记

来源:互联网 发布:数据库系统基础微盘 编辑:程序博客网 时间:2024/05/29 14:32

友元可以分为三种:

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;};
在网上找了很久也没有解决问题,在摸索中,我在类内直接定义友元函数,然后就顺利运行了程序,至于其中的道理,我还需要再摸索摸索!