一、不能重载的运算符:
(1) "."(类成员访问运算符)
(2)" .*"(类成员指针访问运算符)
(3) "::"(域运算符)
(4)"sizeof"(长度运算符)
(5) " ?:"(条件运算符)
(1) "."(类成员访问运算符)
(2)" .*"(类成员指针访问运算符)
(3) "::"(域运算符)
(4)"sizeof"(长度运算符)
(5) " ?:"(条件运算符)
二、运算符
1.算术运算符 + - * / %
2.关系运算符 > < >= <=
3.逻辑运算符 && || !
4.自增 自减 (前++ 后++) (前-- 后--)
5.位运算符 & |
6.赋值运算符 += -= ==
7.输入输出运算符 >> <<
7.其他运算符 () [] * & -> 取负-。。。
2.关系运算符 > < >= <=
3.逻辑运算符 && || !
4.自增 自减 (前++ 后++) (前-- 后--)
5.位运算符 & |
6.赋值运算符 += -= ==
7.输入输出运算符 >> <<
7.其他运算符 () [] * & -> 取负-。。。
三、运算符重载:
1.不能定义新的运算符,只能重载已有的运算符
2.重载之后的运算符的优先级和结合性都不改变
3.不能改变原运算符所需操作符的个数,同时至少要有一个操作数是自定义类型的操作数
4.运算符重载后原语义没消失,只相当于针对特定的类定义了一个新的运算符
1.不能定义新的运算符,只能重载已有的运算符
2.重载之后的运算符的优先级和结合性都不改变
3.不能改变原运算符所需操作符的个数,同时至少要有一个操作数是自定义类型的操作数
4.运算符重载后原语义没消失,只相当于针对特定的类定义了一个新的运算符
四、友元运算符重载和成员运算符重载的主要区别:
1.参数个数不同
2.友元函数没有this指针
3.当运算符的左操作数是一个常数时,就不能利用this指针,应当用友元函数重载,例子见重载减运算符“+”
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++运算符重载的内容,希望对你有所帮助,欢迎在下方评价交流