#include <iostream> using namespace std; //friend 友元,效率的问题 //get 方法和set方法,是标准封装的结果,friend破坏了这种封装。但又带来效率的提高。但又带来了效率的提高。 //有时需要定义一些函数,这些函数不是类的一部分,但又需要频繁地访问类的数据成员,这时可以将这些函数定义为该类的友元函数 class Sprite { friend void fight(Sprite& sp); public: Sprite(int lb):_lifeBlood(lb){} int getLifeblood() { return _lifeBlood; } void setLifeblood(int lb) { _lifeBlood = lb; } private: int _lifeBlood; }; void fight(Sprite& sp) { // sp.setLifeblood(sp.getLifeblood() - 20); // cout << sp.getLifeblood(); sp._lifeBlood = sp._lifeBlood - 20; cout << sp._lifeBlood << endl; } int main() { Sprite sp(100); fight(sp); return 0; }
//声明为谁的友元就可以通过谁的对象访问谁的私有成员 #include <iostream> using namespace std; class Complex { friend Complex operator+(Complex &c1, Complex &c2); public: Complex(double r = 0, double i = 0):real(r), image(i){} void dumpFormat() { cout << "(" << real << "," << image << ")"; } private: double real; double image; }; Complex operator+(Complex &c1, Complex &c2) { Complex c; c.real = c1.real + c2.real; c.image = c1.image + c2.image; return c; } int main() { Complex sum; Complex c1(1, 2); Complex c2(2, 4); sum = c1 + c2; sum.dumpFormat(); return 0; }
全局函数作友元
#include <iostream> #include <math.h> using namespace std; class Point { friend float getdistance(const Point& p1, const Point& p2); public: Point(int x = 0, int y = 0):_x(x), _y(y){} void dumpFormat() { cout << "_x" << _x << "_y" << _y; } private: float _x; float _y; }; float getdistance(const Point& p1, const Point& p2) { float dx = p1._x - p2._x; float dy = p1._y - p2._y; return sqrt(dx * dx + dy * dy); } int main() { Point p1(3, 4); p1.dumpFormat(); Point p2(7, 8); p2.dumpFormat(); cout << "dis:" << getdistance(p1, p2); return 0; }
类成员做友元
#include <iostream> #include <math.h> using namespace std; class Point;//前向声明的问题,前向声明是一种不完全类型的声明,不能定义对象,可以定义指针和引用做参数和返回值,仅用在函数声明 class ManagePoint { public: float getdistance(const Point &p1, const Point &p2); }; class Point { friend float ManagePoint::getdistance(const Point& p1, const Point& p2); public: Point(int x = 0, int y = 0):_x(x), _y(y){} void dumpFormat() { cout << "_x" << _x << "_y" << _y; } private: float _x; float _y; }; float ManagePoint::getdistance(const Point &p1, const Point &p2) { float dx = p1._x - p2._x; float dy = p1._y - p2._y; return sqrt(dx * dx + dy * dy); } int main() { Point p1(3, 4); p1.dumpFormat(); Point p2(7, 8); p2.dumpFormat(); ManagePoint m; cout << "dis:" << m.getdistance(p1, p2); return 0; }
友元类
实际工作中程序员喜欢友元类
#include <iostream> #include <math.h> using namespace std; class Point { friend class ManagePoint; public: Point(int x = 0, int y = 0):_x(x), _y(y){} void dumpFormat() { cout << "_x" << _x << "_y" << _y; } private: float _x; float _y; }; class ManagePoint { public: float getdistance(const Point &p1, const Point &p2); }; float ManagePoint::getdistance(const Point &p1, const Point &p2) { float dx = p1._x - p2._x; float dy = p1._y - p2._y; return sqrt(dx * dx + dy * dy); } int main() { Point p1(3, 4); p1.dumpFormat(); Point p2(7, 8); p2.dumpFormat(); ManagePoint m; cout << "dis:" << m.getdistance(p1, p2); return 0; }