❥关于C++之成员与友元函数重载运算符

类的友元函数,能够直接访问类的私有成员。但它的作用域不是类。

// mytime.h
#ifndef MYTIME3_H_
#define MYTIME3_H_
#include <iostream>
class Time {
private:
	int hours;
	int minutes;
public:
	Time();
	Time(int h, int m = 0);
	void AddMin(int m);
	void AddHr(int h);
	void Reset(int h = 0, int m = 0);
	Time operator+(const Time& t) const;
	Time operator-(const Time& t) const;
	Time operator*(double n) const;
	friend Time operator*(double m, const Time& t) { // 默认类中定义的函数都是内联函数
		return t * m;
	} // 只要是在类声明中直接定义的函数,都自动成为内联函数,无需加inline关键字
	friend std::ostream& operator<<(std::ostream& os, const Time& t);
};
#endif
// mytime.cpp  -- implementing Time methods
#include "mytime3.h"
Time::Time() {
	hours = minutes = 0;
}
Time::Time(int h, int m) {
	hours = h;
	minutes = m;
}
void Time::AddMin(int m) {
	minutes += m;
	hours += minutes / 60;
	minutes %= 60;
}
void Time::AddHr(int h) {
	hours += h;
}
void Time::Reset(int h, int m) {
	hours = h;
	minutes = m;
}
Time Time::operator+(const Time& t) const {
	Time sum;
	sum.minutes = minutes + t.minutes;
	sum.hours = hours + t.hours + sum.minutes / 60;
	sum.minutes %= 60;
	return sum;
}
Time Time::operator-(const Time& t) const {
	Time diff;
	int tot1, tot2;
	tot1 = t.minutes + 60 * t.hours;
	tot2 = minutes + 60 * hours;
	diff.minutes = (tot2 - tot1) % 60;
	diff.hours = (tot2 - tot1) / 60;
	return diff;
}
Time Time::operator*(double mult) const {
	Time result;
	long totalminutes = hours * mult * 60 + minutes * mult;
	result.hours = totalminutes / 60;
	result.minutes = totalminutes % 60;
	return result;
}
std::ostream& operator<<(std::ostream& os, const Time& t) {
    ★→→ t.hours、t.minusts中hours、minutes就是私有成员。但不能像成员函数一样直接通过this隐式调用。
    ★→→ 若不是友元函数,t.hours/t.minutes是不能访问到的,因为是私有的!
	os << t.hours << " hours, " << t.minutes << " minutes";
	return os;
}

可以看到,友元函数,在实现方法时,不能有"friend"关键字,也不能有“Time::”前缀,故友元函数不属于Time类作用域。

不能像成员函数一样直接通过this隐式调用。

若不是友元函数,t.hours/t.minutes是不能访问到的,因为是私有的!

还可以看到,对运算符的重载的成员函数,参数只有一个。而对于运算符重载的友元函数,参数是两个。

 从以上图片,它的重载是一个成员函数,IDE提示:“此运算符函数的参数太多”,可知,*号运算符的成员函数重载,只能有一个参数。同样,将*号替换为[]、=等运算符,也是一样提提示,替换为负号(-)运算符时,可以有1个参数,也可以没有参数。故推出:成员函数重载运算符,最多只能有一个参数。同样地,对于友元函数重载,如果有1个参数,IDE会提示参数太少;如果有3个参数,IDE会提示参数太多。故推出:友元函数重载运算符,有且只能有两个参数

这4种运算符:=、[]、()、->,只能通过成员函数来重载!


重载运算符:作为成员函数还是非成员函数?

 对于很多运算符来说,可以选择使用成员函数或非成员函数来实现运算符重载。一般来说,非成员函数应是友元函数,这样它才能直接访问类的私有数据。例如,Time类的加法运算符在Time类声明中的原型如下:

Time operator+(const Time & t) const; // 成员函数
这个类也可以使用下面的原型:
friend Time operator+(const Time & t1, const Time & t2); // 非成员函数

加法运算符需要两个操作数。对于成员函数版本来说,一个操作数通过this指针隐式地传递,另一个操作数作为函数参数显式地传递;对于友元版本来说,两个操作数都作为参数显式地传递。

T1 = T2 + T3;
转换为下面两个的任何一个:
T1 = T2.operator+(T3); // 成员函数
T1 = operator+(T2, T3); // 非成员函数

对于前面提到的那4种运算符,成员函数是唯一合法的选择。在其他情况下,这两种格式没有太大的区别。有时,根据类设计,使用非成员函数版本可能更好(尤其是为类定义类型转换时)。


另一个示例: 

// vect.h
#ifndef VECTOR_H_
#define VECTOR_H_
#include <iostream>
namespace VECTOR {
	class Vector {
	public:
		enum Mode {RECT, POL}; // RECT for rectangular, POL for Polar modes
	private:
		double x;          // horizontal value
		double y;          // vertical value
		double mag;        // length of vector
		double ang;        // direction of vector in degrees
		Mode mode;         // RECT or POL
		// private methods for setting values
		void set_mag();
		void set_ang();
		void set_x();
		void set_y();
	public:
		Vector();
		Vector(double n1, double n2, Mode form = RECT);
		void reset(double n1, double n2, Mode form = RECT);
		~Vector();
		double xval() const { // report x value
			return x;
		}
		double yval() const { // report y value
			return y;
		}
		double magval() const { // report magnitude
			return mag;
		}
		double angval() const { // report angle
			return ang;
		}
		void polar_mode(); // set mode to POL
		void rect_mode(); // set mode to RECT
		// operator overloading
		Vector operator+(const Vector& b) const;
		Vector operator-(const Vector& b) const;
		Vector operator-() const;
		Vector operator*(double n) const;
		// friends
		friend Vector operator*(double n, const Vector& a);
		friend std::ostream& operator<<(std::ostream& os, const Vector& v);
	};
}   // end namespace VECTOR
#endif
// vect.cpp
#include <cmath>
#include "vect.h"// 头文件包含了includes <iostream>,现在全部include到这里了。
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;
namespace VECTOR { // 这里也要用VECTOR名称空间,否则下面的Vector报错为“未定义的标识符”
	// compute degrees in one radian
	const double Rad_to_deg = 45.0 / atan(1.0); // should be about 57.2957795130823
	// private methods
	// calculates magnitude from x and y
	void Vector::set_mag() {
		mag = sqrt(x * x + y * y);
	}
	void Vector::set_ang() {
		if (x == 0.0 && y == 0.0)
			ang = 0.0;
		else
			ang = atan2(y, x);
	}
	// set x from polar coordinate
	void Vector::set_x() {
		x = mag * cos(ang);
	}
	// set y from polar coordinate
	void Vector::set_y() {
		y = mag * sin(ang);
	}
	// public methods
	Vector::Vector() {          // default constructor
		x = y = mag = ang = 0.0;
		mode = RECT;
	}
	// construct vector from rectangular coordinates if form is r
	// (the default) or else from polar coordinates if form is p
	Vector::Vector(double n1, double n2, Mode form) {
		mode = form;
		if (form == RECT) {
			x = n1;
			y = n2;
			set_mag();
			set_ang();
		} else if (form == POL) {
			mag = n1;
			ang = n2 / Rad_to_deg;
			set_x();
			set_y();
		} else {
			cout << "Incorrect 3rd argument to Vector() -- ";
			cout << "vector set to 0\n";
			x = y = mag = ang = 0.0;
			mode = RECT;
		}
	}
	// reset vector from rectangular coordinates if form is RECT (the default)
	// or else from polar coordinates if form is POL
	void Vector::reset(double n1, double n2, Mode form) {
		mode = form;
		if (form == RECT) {
			x = n1;
			y = n2;
			set_mag();
			set_ang();
		} else if (form == POL) {
			mag = n1;
			ang = n2 / Rad_to_deg;
			set_x();
			set_y();
		} else {
			cout << "Incorrect 3rd argument to Vector() -- ";
			cout << "vector set to 0\n";
			x = y = mag = ang = 0.0;
			mode = RECT;
		}
	}
	Vector::~Vector() {// destructor
	}
	void Vector::polar_mode() {// set to polar mode
		mode = POL;
	}
	void Vector::rect_mode() {// set to rectangular mode
		mode = RECT;
	}
	// operator overloading
	// add two Vectors
	Vector Vector::operator+(const Vector& b) const {
		return Vector(x + b.x, y + b.y);
	}
	// subtract Vector b from a
	Vector Vector::operator-(const Vector& b) const {
		return Vector(x - b.x, y - b.y);
	}
	// reverse sign of Vector
	Vector Vector::operator-() const {
		return Vector(-x, -y);
	}
	// multiply vector by n
	Vector Vector::operator*(double n) const {
		return Vector(n * x, n * y);
	}
	// friend methods
	// multiply n by Vector a
	Vector operator*(double n, const Vector& a) {
		return a * n;
	}
	// display rectangular coordinates if mode is RECT,
	// else display polar coordinates if mode is POL
	std::ostream& operator<<(std::ostream& os, const Vector& v) {
		if (v.mode == Vector::RECT) // 友元函数,不在类作用域,所以必须Vector::RECT
			os << "(x,y) = (" << v.x << ", " << v.y << ")";
		else if (v.mode == Vector::POL) {
			os << "(m,a) = (" << v.mag << ", "
				<< v.ang * Rad_to_deg << ")";
		} else
			os << "Vector object mode is invalid";
		return os;
	}
}  // end namespace VECTOR

从最后两个友元重载运算符函数中,可以看到,没有“Vector::”前缀,也没有“friend”关键字。从最后一个重载运算符函数中可以看到,虽然RECT/POL是私有的,但也可访问,因为是友元函数,但要加上“Vector::”前缀,因为友元函数不在类作用域。总之就是:用友元函数重载,友元函数可以访问类的私有成员,但不属于类作用域。由于整体被namespace VECTOR{}包围,所以不用VECTOR::Vector::RECT去访问了。 


用成员函数VS友元函数重载加法运算符: 

成员函数版本:

Stonewt Stonewt::operator+(const Stonewt & st) const {
    double pds = pounds + st.pounds;
    Stonewt sum(pds);
    return sum;
}

友元函数版本:

Stonewt Stonewt::operator+(const Stonewt & st1, const Stonewt & st2) {
    double pds = st1.pounds + st2.pounds;
    Stonewt sum(pds);
    return sum;
}

将每一种加法都转换为相应的函数调用:

total = jennySt + bennySt;
被转换为:
total = jennySt.operator+(bennySt); // 成员函数
或:
total = operator+(jennySt, bennySt); // 友元函数

如果加法的第一个加数是double类型,则成员函数显然不能胜任了,只可能是友元函数重载:

total = N + bennySt;
被转换为:
total = N.operator+(bennySt); // No!Impossible!
只可能:
total = operator+(N, bennySt); // Yes!Can!
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

itzyjr

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值