程序员求职经验分享与学习资料整理平台

网站首页 > 文章精选 正文

c++ 专题 运算符重载

balukai 2025-03-11 13:09:54 文章精选 71 ℃

一 概念

1 概念:所谓重载就是重新赋予新的含义,函数重载就是对一个已有的函数赋予新的定义,使之实现新的功能.

2 为什么会用运算符重载机制?

当编译器不知道如何运算用户自定义类型的时候,编译器就提供了一种机制,让用户自己完成.

复数:3+4i

Complex

c1 - (c2 + c3)

c1.sub (c2.add (c3))

运算符重载的本质是一个函数

3 运算符重载的语法形式

1全局函数 Complex operator+( Complex &c1,Complex &c2)//二元运算符

2类成员函数 Complex operator-(Complex &c2)//一元

(一个运算符被重载后,原有的意义没有失去,只是定义了相对一特定类的一个新的运算符)

4 运算符重载的2种方法

用成员函数/友员函数重载运算符

运算符函数可以重载为成员函数或者友员函数,关键区别在于成员函数有this指针,友员函数没有this指针,不管成员函数,还有友员函数函数重载,运算符的使用方法相同.但是,传递参数的方式不同,实现代码不同,应用场合不同.要根据具体要求,看返回的是,引用,指针,元素.

二 例子

双目操作符

#include //操作符重载

using namespace std;

class Complex {

public:

Complex (int r = 0, int i = 0) :

m_r (r), m_i (i) {}

void print (void) const {

cout << m_r << '+' << m_i << 'i' << endl;

}

// 第一个const:返回右值,返回的对象不能接受赋值。

// 第二个const:支持常量型右操作数,可以接受有常属性的变量为参数,将引用声明为常引用才能指向常变量,常引用也可以指向非常变量。

// 第三个const:支持常量型左操作数,使this指针变为常指针,也可以指向有常属性的变量。

const Complex operator+ (const Complex& r) const { //操作符重载的成员函数形式:L.operator+(R)

return Complex (m_r + r.m_r, m_i + r.m_i);

}

private:

int m_r;

int m_i;

friend const Complex operator- (const Complex&,

const Complex&); //将该函数声明为友员这样该函数就可以访问类的私有部分

};

const Complex operator- (const Complex& l,const Complex& r) { //操作符重载的全局函数形式,::operator-(L,R)

return Complex (l.m_r - r.m_r, l.m_i - r.m_i);

}

int main (void) {

const Complex c1 (1, 2);

c1.print ();

const Complex c2 (3, 4);

Complex c3 = c1 + c2; // c3 = c1.operator+ (c2)

c3.print (); // 4+6i

// (c1 + c2) = c3;

c3 = c2 - c1; // c3 = ::operator- (c2, c1)

c3.print (); // 2+2i

return 0;

}

---------------------------------------------

2. +=/-=/*=...

左变右不变。

表达式的值是左值,左操作数的引用。

(a += b) = c;

例子:

#include

using namespace std;

class Complex {

public:

Complex (int r = 0, int i = 0) :

m_r (r), m_i (i) {}

void print (void) const {

cout << m_r << '+' << m_i << 'i' << endl;

}

//成员函数形式:

Complex& operator+= (const Complex& r) { //返回的是左操作数的引用,是可以赋值的,所以最前面不加const,调用该函数的左操作数是要改变的也就是this会修改,也就是调用函数的参数被修改,所以{前也不加const。

m_r += r.m_r;

m_i += r.m_i;

return *this;//返回该调用操作数

}

//全局函数形式:(把定义与声明合二为一,因为有friend,所以是全局函数)

friend Complex& operator-= (Complex& l,

const Complex& r) { //第一个参数l为左操作数引用,可被修改,所以不加const,而第二个参数r为右操作数,值不会被修改,所以加const。

l.m_r -= r.m_r;

l.m_i -= r.m_i;

return l;//返回左操作数

}

private:

int m_r;

int m_i;

};

int main (void) {

Complex c1 (1, 2), c2 (3, 4);

c1 += c2; // c1.operator+= (c2)

c1.print (); // 4+6i

Complex c3 (5, 6);

(c1 += c2) = c3;//返回的是左操作数c1的引用,把c3赋值给c1.

c1.print (); // 5+6i

c1 -= c2; // ::operator-= (c1, c2)

c1.print (); // 2+2i

(c1 -= c2) = c3;

c1.print (); // 5+6i;

return 0;

}

---------------------------------------------------

3. <>

int i = 10;

float f = 1.23;

Complex c (...);

cout << c << i << f << endl;

cin >> c;

左操作数ostream/istream类型,不能是常量,不能拷贝。

右操作数自定义类型,对于<<可以是常量,对于>>不能是常量。

表达式的值是左操作数的引用。

::operator<< (cout, c).operator<< (i).operator<< (f).operator<< (endl);

说明:istream 和ostream 是c++的预定义流类

cin 是istream的对象,cout 是ostream的对象

运算符<<由ostream重载为插入操作,用于输出基本数据类型

运算符>>由istream重载为提取操作,用于输入基本数据类型

用友员函数重载<<,>>输出和输入用户自定义的数据类型

例子:

#include

//输入与输出。

using namespace std;

class Complex {

public:

Complex (int r = 0, int i = 0) :

m_r (r), m_i (i) {}

void print (void) const {

cout << m_r << '+' << m_i << 'i' << endl;

}

//全都使用全局函数的形式

friend ostream& operator<< (ostream& os, //返回的是左操作数的引用

const Complex& r) { //因为右操作数是要输出的,所以右操作数可以是常量,所以声明为常引用,加const,也可做非常变量的引用。

return os << r.m_r << '+' << r.m_i << 'i';//返回左操作数的引用

}


friend istream& operator>> (istream& is,//返回的是左操作数的引用

Complex& r) { //因为右操作数是要输入的,所以右操作数不能是常量,不加const

return is >> r.m_r >> r.m_i; //返回左操作数的引用

}

private:

int m_r;

int m_i;

};

int main (void) {

Complex c1 (1, 2), c2 (3, 4);

cout << c1 << endl << c2 << endl;

// ::operator<<(::operator<<(cout,c1).operator<<(

// endl),c2).operator<<(endl);

cin >> c1 >> c2;

cout << c1 << endl << c2 << endl;

return 0;

}

三、单目操作符

1.-(取负)/!/~

操作数不变。

表达式的值是右值。

例子:

#include

using namespace std;

class Complex {

public:

Complex (int r = 0, int i = 0) :

m_r (r), m_i (i) {}

void print (void) const {

cout << m_r << '+' << m_i << 'i' << endl;

}


//成员函数形式:

const Complex operator- (void) const { //只有一个操作数,且作为调用参数,所以没有形参。因为返回的是右值,是常量,所以要加const(第一个const)。操作数不改变,就是this不可改变,也加const(第二个const)。

return Complex (-m_r, -m_i);//返回的是右值(常数)

}

//全局函数形式:

friend const Complex operator~ (

const Complex& o) { //就一个操作数,且不改变,加const

return Complex (o.m_i, o.m_r);//交换实部与虚部

}

private:

int m_r;

int m_i;

};

int main (void) {

const Complex c1 (1, 2);

Complex c2 = -c1; // c2=c1.operator-()

c2.print (); // -1+-2i

Complex c3 = ~c1; // c3=::operator~(c1)

c3.print (); // 2+1i;

return 0;

}



2.前++/前--

操作数变。

表达式的值是运算以后的值。

表达式的值是左值,操作数的引用。

(++i) = 100;

++++++i;

例子:

#include

using namespace std;

class Complex {

public:

Complex (int r = 0, int i = 0) :

m_r (r), m_i (i) {}

void print (void) const {

cout << m_r << '+' << m_i << 'i' << endl;

}

//成员函数形式:

Complex& operator++ (void) { //只有一个操作数,且该操作数是改变的,所以前{不加const。而返回的是一个可以改变的左值,既操作数的引用,所以可以改变,最前面不加const。

++m_r;

++m_i;

return *this;

}

//全局函数形式:

friend Complex& operator-- (Complex& o) { //返回的是操作数本身,既引用。

--o.m_r;

--o.m_i;

return o; //返回操作数的引用。

}

private:

int m_r;

int m_i;

};

int main (void) {

Complex c1 (1, 2);

Complex c2 = ++c1; // c2=c1.operator++() //成员函数形式

c1.print (); // 2+3i

c2.print (); // 2+3i

(++c1) = Complex (10, 20);

c1.print (); // 10+20i;

(++++++c1).print (); // 13+23i

c2 = --c1; // c2=::operator--(c1) //全局函数形式

c1.print (); // 12+22i

c2.print (); // 12+22i

return 0;

}

-----------------------------------------------------------

3.后++/后--

操作数变。

表达式的值是运算以前的值。

表达式的值是右值。

例子:

#include

using namespace std;

class Complex {

public:

Complex (int r = 0, int i = 0) :

m_r (r), m_i (i) {}

void print (void) const {

cout << m_r << '+' << m_i << 'i' << endl;

}

const Complex operator++ (int) { //返回的是一个右值,不变,所以最前面家一个const。int是哑元,占个位置,没有实际意义,与有形参的进行匹配。

Complex old (*this);

++m_r;

++m_i;

return old;

}

friend const Complex operator--(Complex& o, int) { //操作数会被修改,所以用引用,不加const。Int是占位置的,没有实际意义


Complex old (o);

--o.m_r;

--o.m_i;

return old;

}

private:

int m_r;

int m_i;

};

int main (void) {

Complex c1 (1, 2);

Complex c2 = c1++; // c2=c1.operator++(0) //成员函数形式

c1.print (); // 2+3i;

c2.print (); // 1+2i;

// (c1++) = c2;

// c1++++++;

c2 = c1--; // c2=::operator--(c1,0) //全局函数形式

c1.print (); // 1+2i //c1进行了--,得到新的值。

c2.print (); // 2+3i //c2得到表达式之前的值。

return 0;

}

运算符重载远远不止上述几个例子,还有很多,因为时间原因这里不再赘述.


三 关于操作符重载的限制

1.至少有一个操作数是类类型的。

int a = 10, b = 20;

int c = a + b; // 200

int operator+ (int a, int b) {

return a * b;

} // ERROR !

2.不是所有的操作符都能重载。

:: - 作用域限定

. - 直接成员访问

.* - 直接成员指针解引用

?: - 三目运算符

sizeof - 获取字节数

typeid - 获取类型信息

3.不是所有的操作符都可以用全局函数的方式实现。

= - 拷贝赋值

[] - 下标

() - 函数

-> - 间接成员访问

4.不能发明新的操作符,不能改变操作数的个数

x ** y; // x^y

# @

5不能改变运算符的优先级和结合性

接下来会根据我的进度,不定期更新c++相关知识,谢谢大家的关注

最近发表
标签列表