C++运算符重载详细解说及代码编写

本文详细介绍了C++中的运算符重载概念,包括不能重载的运算符、算术运算符、关系运算符等的重载方法,并通过具体示例展示了如何实现运算符的重载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、不能重载的运算符:
  (1) "."(类成员访问运算符)
  (2)" .*"(类成员指针访问运算符)
  (3) "::"(域运算符)
  (4)"sizeof"(长度运算符)
  (5) " ?:"(条件运算符)
二、运算符
  1.算术运算符    +   -   *   /    %
  2.关系运算符    >   <   >=  <= 
  3.逻辑运算符    &&  ||  !
  4.自增 自减    (前++  后++) (前--  后--)
  5.位运算符      &   |  
  6.赋值运算符    += -=   ==
  7.输入输出运算符 >>  <<
  7.其他运算符    ()  []  *   &   ->  取负-。。。
三、运算符重载:
  1.不能定义新的运算符,只能重载已有的运算符
  2.重载之后的运算符的优先级和结合性都不改变
  3.不能改变原运算符所需操作符的个数,同时至少要有一个操作数是自定义类型的操作数
  4.运算符重载后原语义没消失,只相当于针对特定的类定义了一个新的运算符
四、友元运算符重载和成员运算符重载的主要区别:
  1.参数个数不同
  2.友元函数没有this指针
  3.当运算符的左操作数是一个常数时,就不能利用this指针,应当用友元函数重载,例子见重载减运算符“+”
  若运算符是一元的,则参数表为空,此时当前对象作为此运算符的单操作数;
  若运算符是二元的,则参数表中有一个操作数,此时当前对象作为此运算符的左操作数,参数表中的操作数作为此运算符的右操作数,以此类推。
五、源代码示例
#include<iostream>
using namespace std;
struct st
{
	int x;
};
st s;
class A
{
	int x;
	int arr[10];
public:
	A() { x = 0; cout << "调用无参构造" << endl; }
	A(int x) :x(x) { cout << "调用有参构造" << endl; }
	A(const A& other) :x(other.x) { cout << "调用拷贝构造" << endl; }
	~A() { cout << "调用析构函数" << endl; }
	//重载算术运算符  
	A operator+(const A& other);
	A operator-(const A& other);
	A operator*(const A& other);
	A operator/(const A& other);
	A operator%(const A& other);
	//重载关系运算符
	friend bool operator>(const A& a, const A& b);
	friend bool operator<(const A& a, const A& b);
	friend bool operator>=(const A& a, const A& b);
	friend bool operator<=(const A& a, const A& b);
	//自增自减
	A& operator++();//前++
	A operator++(int);//后++ 参数int不需要传参 与前++区分开
	A& operator--();//前--
	A operator--(int);//后--
	//赋值运算符
	A& operator+=(const A& other);
	A& operator-=(const A& other);
	bool operator==(const A& other);
	//输入输出运算符
	friend istream& operator >> (istream& is, A& other);//不可以用const A& 否则报错 因为other应为可修改的变量 const常量会导致other不可修改
	friend ostream& operator << (ostream& os, const A& other);
	//其他运算符() [] * &  ->  取负 -
	void operator()(int x, int y);
	int& operator[](size_t index);
	int& operator*();
	int* operator&();
	st* operator->();
	A operator-();

};
//重载算术运算符 
//+运算符重载
A A::operator+(const A& other)
{
	return A(this->x+other.x);
}
//-运算符重载
A A::operator-(const A&other)//类外定义成员函数 函数名前面加上类名::
{
	return A(this->x - other.x);
}

//*运算符重载
A A::operator*(const A&other)
{
	return A(this->x*other.x);
}

//  /运算符重载
A A::operator/(const A&other)
{
	return A(this->x / other.x);
}

//%运算符重载
A A::operator%(const A&other)
{
	return A(this->x%other.x);
}
//重载关系运算符
//>运算符重载
bool operator>(const A&a, const A&b)
{
	return a.x > b.x;
}

//<运算符重载
bool operator<(const A&a, const A&b)
{
	return a.x < b.x;
}

//>=运算符重载
bool operator>=(const A&a, const A&b)
{
	return a.x >= b.x;
}

//<=运算符重载
bool operator<=(const A&a, const A&b)
{
	return a.x <= b.x;
}
//自增自减
//前++
A& A::operator++()
{
	++this->x;
	return *this;//返回值是引用,即返回的是对象本身,而不是临时对象
}
//返回引用和不返回引用 区别在于是否需要产生临时对象 ++++++a时,返回不是引用会不能连续前++ 如++++++a结果依旧为 1
//返回引用保证了地址在上一次前++处 也就意味着是在前一次++的基础上再++  如++++++a结果为 3
//参数int不需要传参  int用于区分前后++
//后++
A A::operator++(int)
{
	return A(this->x++);//不能返回对象本身 应返回临时对象(返回类型不是引用) 调用完后被析构先读取到this->x  表现出延迟性
}
//前--
A& A::operator--()
{
	--this->x;
	return *this;
}
//后--
A A::operator--(int)
{
	return A(this->x--);
}
//赋值运算符
//+=运算符
A& A::operator+=(const A& other)
{
	this->x += other.x;
	return *this;
}
//-=运算符
A& A::operator-=(const A& other)
{
	this->x -= other.x;
	return *this;
}
//==运算符
bool A::operator==(const A& other)
{
	return this->x == other.x;
}
//重载输入输出
//重载>>
istream& operator >> (istream& is, A& other)
{
	is >> other.x;
	return is;
}
//重载<<
ostream& operator << (ostream& os, const A& other)
{
	os << other.x;
	return os;
}

//其他运算符    ()  []  *   &   ->  取负-
//()
void A::operator()(int x, int y)
{
	cout << "假装自己是函数名" << endl;
	cout << x << '\t' << y << endl;
}

//[]
int& A::operator[](size_t index)
{
	return arr[index];
}
//*
int& A::operator*()
{
	return arr[0];//*arr 数组的地址
}
//&
int* A::operator&()//重载& 取地址  要求返回地址  这个地址是什么地址都可以
{
	return arr;
}
//->
st* A::operator->()//一般用于结构体指针 对象指针
{
	return &s;//返回结构体指针
}
A A::operator-()
{
	A a(-this->x);
	return a;
}
int main()
{
	A a, b,c;
	++++++a;
	cout << "++++++a=" << a << endl;
	b=a++;
	cout << "b=" << b <<'\t'<< "a=" << a << endl;
	c = a + b;
	cout << "c=a+b=" << c << endl;
	c = a - b;
	cout << "c=a-b=" << c << endl;
	c = a*b;
	cout << "c=a*b=" << c << endl;
	c = a / b;
	cout << "c=a/b=" << c << endl;
	c = a % b;
	cout << "c=a%b=" << c << endl;
	a += b;
	cout << "a+=b:" << a << endl;
	a -= b;
	cout << "a-=b:" << a << endl;
	if (a == b) { cout << "a==b"<<endl; }
	else { cout << "a!=b" << endl; }
	a(3,4);
	a[2] = 2;
	cout << "a[2]=" << a[2] << endl;
	a[0] = 1;
	*a = a[0];
	cout << "*a=" << *a << endl;
	cout << "&a=" << &a << endl;
	a->x = 8;
	cout << "a->x=" << a->x << endl;
	cin.get();
	return 0;
}
以上便是C++运算符重载的内容,希望对你有所帮助,欢迎在下方评价交流
visual basic 2005 技术内部中第六章第七节运算符重载代码。 operator部分: Module Module1 Sub Main() End Sub End Module Public Structure Fraction 'Read-Only fields Private num As Long Private den As Long 'Read-Only properties Public ReadOnly Property Numerator() As Long Get Return num End Get End Property Public ReadOnly Property Denominator() As Long Get Return den End Get End Property Sub New(ByVal numerator As Long, ByVal denominator As Long) 'Normalize the numerator and denominator If numerator = 0 Then numerator = 1 ElseIf denominator < 0 Then numerator = -numerator denominator = -denominator End If Dim div As Long = GCD(numerator, denominator) num = numerator \ div den = denominator \ div End Sub 'the greatest common divisor of two numbers (helper method) Private Function GCD(ByVal n1 As Long, ByVal n2 As Long) As Long n1 = Math.Abs(n1) n2 = Math.Abs(n2) Do 'ensure that n1>n2 If n1 < n2 Then Dim tmp As Long = n1 n1 = n2 n2 = tmp End If n1 = n1 Mod n2 Loop While n1 <> 0 End Function 'override ToString to provide a textual representation of the fraction Public Overrides Function ToString() As String If num = 0 OrElse den = 1 Then Return num.ToString Else Return String.Format("{0}/{1}", num, den) End If End Function Public Shared Operator +(ByVal f1 As Fraction, ByVal f2 As Fraction) As Fraction 'a/b+c/d=(a*d+b*c)/(b*d) Return New Fraction(f1.num * f2.den + f2.num * f1.den, f1.den * f2.den) End Operator Public Shared Operator -(ByVal f1 As Fraction, ByVal f2 As Fraction) 'a/b-c/d=(a*d-b*c)/(b*d) Return New Fraction(f1.num * f2.num, f1.den * f2.den) End Operator Public Shared Operator *(ByVal f1 As Fraction, ByVal f2 As Fraction) 'a/b * c/d=(a*c)/(b*d) Return New Fraction(f1.num * f2.num, f1.den * f2.den) End Operator Public Shared Operator /(ByVal f1 As Fraction, ByVal f2 As Fraction) Return New Fraction(f1.num * f2.den, f1.den * f2.num) End Operator End Structure
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值