操作符重载应该设计成类成员函数还是普通非成员函数?

四小点:


1.=,[ ],( ),->这四个操作符重载必须定义为类成员函数。


2.复合赋值操作符重载通常应定义为类成员函数。复合赋值操作符如*=,+=等。但是不一定得这么做。


3.改变对象状态或者与给定对象联系紧密的操作符,如++,---,*等,通常定义为类成员函数。


4.对称操作符,如算术,相等,关系,位操作符,最好定义成普通非成员函数。


下面是自己N年之前写过的一个3D类:

#include <iostream>
using namespace std;

class Vector3
{
public:
	double x,y,z;
public:
	Vector3(double nx = 0.0,double ny = 0.0,double nz = 0.0):x(nx),y(ny),z(nz){}
	Vector3(const Vector3 &rhs)
	{
		operator = (rhs);
	}
	Vector3& operator =(const Vector3 &rhs)
	{
		if(this != &rhs)
		{
			x = rhs.x;
			y = rhs.y;
			z = rhs.z;
		}
		return *this;
	}

	void zero()
	{
		x = 0.0;
		y = 0.0;
		z = 0.0;
	}

	Vector3 operator - () const
	{
		return Vector3(-x,-y,-z);
	}

	Vector3& operator += (const Vector3 &rhs)
	{
		x += rhs.x;
		y += rhs.y;
		z += rhs.z;
		return *this;
	}

	Vector3& operator -= (const Vector3 &rhs)
	{
		x -= rhs.x;
		y -= rhs.y;
		z -= rhs.z;
		return *this;
	}

	Vector3& operator *= (double a)
	{
		x *= a;
		y *= a;
		z *= a;
		return *this;
	}

	Vector3& operator /= (double a)
	{
		double oneOverA = 1 /a; 
		x *= oneOverA;
		y *= oneOverA;
		z *= oneOverA;
		return *this;
	}

	void normalize()  //标准化
	{
		double Mag  = sqrt(x * x + y * y + z * z);
		if(Mag > 0.0)
		{
			double oneOverMag = 1.0 / Mag;
			x *= oneOverMag;
			y *= oneOverMag;
			z *= oneOverMag;
		}
	}

	void print() const
	{
		cout << x << " " << y << " " << z << endl;
	}
};

bool operator ==(const Vector3 &lhs, const Vector3 &rhs)
{
	return lhs.x == rhs.x && lhs.y == rhs.y && lhs.z ==rhs.z;
}

bool operator !=(const Vector3 &lhs, const Vector3 &rhs)
{
	return !(lhs == rhs);
}

Vector3 operator + (const Vector3 &lhs, const Vector3 &rhs)
{
	Vector3 temp(lhs);
	temp += rhs;
	return temp;
}

Vector3 operator - (const Vector3 &lhs, const Vector3 &rhs)
{
	Vector3 temp(lhs);
	temp -= rhs;
	return temp;
}

Vector3 operator * (const Vector3 &lhs, double a)  //标量右乘
{
	Vector3 temp(lhs);
	temp *= a;
	return temp;
}

Vector3 operator * (double a,const Vector3 &rhs)  //标量左乘
{
	Vector3 temp(rhs);
	temp *= a;
	return temp;
}

Vector3 operator * (const Vector3 &lhs, const Vector3 &rhs)  //点乘
{
	return Vector3(lhs.x * rhs.x, lhs.y * rhs.y, lhs.z * rhs.z);
}

Vector3 crossProduce(const Vector3 &lhs, const Vector3 &rhs) //叉乘
{
	return Vector3(
		lhs.y * rhs.z - lhs.z * rhs.y,
		lhs.z * rhs.x - lhs.x * rhs.z,
		lhs.x * rhs.y - lhs.y * rhs.x);
}


double vectorMag(const Vector3 &a)  //求模
{
	return sqrt(a.x * a.x + a.y* a.y + a.z * a.z);
}

double Distance(const Vector3 &lhs, const Vector3 &rhs) //求距离
{
	Vector3 temp = rhs - lhs;
	return vectorMag(temp);
}


int main()
{
	Vector3 a(1,2,3);
	Vector3 b(1,1,1);
	Vector3 c = a * 3;
	c.print();
	Vector3 d = 3 * a;
	d.print();
	Vector3 e = -a;
	e.print();
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值