友元函数的正确用法和滥用方法

来源:互联网 发布:阿里云域名解析 速度 编辑:程序博客网 时间:2024/05/10 10:53
  1. 重点:友元函数用于左移(<<)和右移(>>)运算符的重载, 而其他的运算符都要写成成员函数,最好不要滥用友元函数。

  2. 第一种情况:所有函数写在类的内部
  3. 代码如下:
  4. #include "stdafx.h"#include "iostream"using namespace std;template <typename T>class Complex{<span style="color:#ff0000;">friend Complex operator-(const Complex &c1, const Complex &c2)//这种情况下就属于滥用友元函数{Complex tmp(c1.a - c2.a, c1.b - c2.b);return tmp;}</span>//重载<<<span style="color:#009900;">friend ostream& operator<<(ostream &out, const Complex &obj);{out << obj.a << "+" <<obj.b << "i" << endl;return out;}</span>public:Complex(T a, T b){this->a = a;this->b = b;}Complex operator+(const Complex &obj){Complex tmp(a+obj.a, b+obj.b);return tmp;}private:T a;T b;};int _tmain(int argc, _TCHAR* argv[]){Complex<int> c1(1, 2), c2(3, 4);Complex<int> c3 = c1 + c2;cout << c3;//滥用友元函数{Complex<int> c4 = c1 - c2;cout <<"c4 = "<< c4;}printf("Hello...\n");system("pause");return 0;}


  5. 第二种情况:所有函数在类的外部,但在一个CPP里面
  6. 代码如下:
  7. <pre name="code" class="cpp">#include "stdafx.h"#include "iostream"using namespace std;//一下这部分就是为了解决operator-友元函数问题所要加的部分<span style="color:#ff0000;">template <typename T>class Complex;template <typename T>Complex<T> operator-(const Complex<T> &c1, const Complex<T> &c2);</span>template <typename T>class Complex{<span style="color:#ff0000;">friend Complex<T> operator-<T>(const Complex<T> &c1, const Complex<T> &c2);//这里多加了很多<T></span>//重载<<<span style="color:#006600;background-color: rgb(255, 255, 255);">friend ostream& operator<< <T> (ostream &out, const Complex &obj);//这里比在内部的时候多加了<T></span>public:Complex(T a, T b);Complex operator+(const Complex &obj);private:T a;T b;};//滥用 友元函数<span style="color:#ff0000;">template <typename T>Complex<T> operator-(const Complex<T> &c1, const Complex<T> &c2){Complex<T> tmp(c1.a - c2.a, c1.b - c2.b);return tmp;}</span><span style="color:#006600;">template <typename T>ostream& operator<<(ostream &out, const Complex<T> &obj)//由于友元函数不是类的成员函数,是全局函数,所以无需域作用符{out << obj.a << "+" << obj.b << "i" << endl;return out;}</span>//构造函数的实现 写在了类的外部template <typename T>Complex<T>::Complex(T a, T b){this->a = a;this->b = b;}//本质是 : 模板是两次 编译生成的第一次生成的函数头和第二次生成的函数头不一样//成员函数 实现 +运算符重载template <typename T>Complex<T> Complex<T>::operator+(const Complex<T> &obj){Complex tmp(a + obj.a, b + obj.b);return tmp;}int _tmain(int argc, _TCHAR* argv[]){//需要把模板类 进行具体化以后  才能定义对象  C++编译器要分配内存Complex<int> c1(1, 2), c2(3, 4);Complex<int> c3 = c1 + c2;cout << c3;//滥用友元函数{Complex<int> c4 = c1 - c2;cout << "c4 = " << c4;}printf("Hello...\n");system("pause");return 0;}

    问题如下:大家可以比较仔细比较一下这两段代码,当友元函数在类外部实现的时候是很繁琐的,一般不建议用,这里只是提供一种解决方法,即使是第三种情形:所有函数写在类的内部(分别在.h和.cpp),滥用情况也可以解决,方法与第二种类似!
0 0
原创粉丝点击