09 操作符重载

时间:2022-02-03 17:46:08

操作符重载:

自定义类型的对象进行运算时需要实现操作符重载函数

1

2 #include<iostream>

3

4 using namespacestd;

5

6 class Student {

7 private:

8 int id;

9 int age;

10

11 public:

12 Student(intid, int age) {

13 this->id= id;

14 Student::age = age;

15 }

16 };

17

18 int main(void)

19 {

20 Student a(0,88), b(1, 11), c(0, 0);

21

22 c = a + b; // 通过错误信息可得出,a+b需要调用operator+函数.

23 return 0;

24 }


[root@localhost09operator]# g++ 01test.cpp

01test.cpp: Infunction ‘int main()’:

01test.cpp:23:8:error: no match for ‘operator+’ (operand types are ‘Student’and ‘Student’)

c = a + b;

^



想要对象间可以直接运算,则实现相应的操作符函数即可:


#include <iostream>


using namespace std;


class Student {

private:

int id;

int age;


public:

Student(int id, intage);

Studentoperator+(Student s);

void show();

};


int main(void)

{

Student a(0, 88),b(1, 11), c(0, 0);

c = a + b; //"a+b"实际就是a.operator+(b),a对象可以在函数通过this指针获取到

c.show();

return 0;

}


void Student::show()

{

cout << "id" << id << endl;

cout << "age" << age << endl;

}

Student::Student(intid, int age)

{

this->id = id;

Student::age = age;

}


//第一个Student表示返回值类型,第二个为指定所属类名,第三个参数类型

StudentStudent::operator+(Student s)

{

Student tmp(0, 0);


tmp.id = this->id+ s.id;

tmp.age = this->age+ s.age;

return tmp;

}


编译执行后输出:

[root@localhost09operator]# ./a.out

id 1

age 99



也可以实现”*/ % [] ++ --”的运算符.

void operator++(void) //++a;

void operator++(int) //a++; 后面的int参数只用于区分前++,还是后++用的


通过操作符重载,可以把链表实现数组的作用.

例如QT里的QList操作符函数:


bool

operator!=(constQList<T> &other) const

QList<T>

operator+(constQList<T> &other) const

QList<T> &

operator+=(constQList<T> &other)

QList<T> &

operator+=(constT &value)

QList<T> &

operator<<(constQList<T> &other)

QList<T> &

operator<<(constT &value)

QList<T> &

operator=(constQList<T> &other)

QList &

operator=(QList<T>&&other)

bool

operator==(constQList<T> &other) const

T &

operator[](inti)

const T &

operator[](inti) const



“=”的操作符重载:

#include <iostream>


using namespace std;


class MyCls {

private:

int id;


public:

MyCls(const int id= 0)

{

this->id = id;

}

void show()

{

cout << id<< endl;

}

};


int main(void)

{

MyCls a(88), b;


b = a;

b.show();


return 0;

}


编译执行后输出:

[root@localhost09operator]# ./a.out

88


结果表明已经把对象a的值赋于b对象了,实际上这样是有漏洞的。



#include <iostream>


using namespace std;

class MyCls {

public:

int *p;


MyCls(const int id= 0)

{

p = new int(id);

}

void show()

{

cout << *p<< endl;

}

};


int main(void)

{

MyCls a(88), b;


b = a;

b.show(); //这里输出的值为88

*(a.p) = 7788;

b.show(); //这里输出的值为7788,表示”b=a”时,仅仅把a.p指向的地址赋给b.p指针而已,这样会让两个对象共用一个空间.


return 0;

}


编译执行后输出:

[root@localhost09operator]# ./a.out

88

7788



这种情况下,需重新实现”=”操作符函数


#include <iostream>


using namespace std;

class MyCls {

public:

int *p;


MyCls(const int id= 0)

{

p = new int(id);

}

void show()

{

cout << *p<< endl;

}

voidoperator=(MyCls s)

{

*p = *(s.p);

}

};


int main(void)

{

MyCls a(88), b;


b = a;

b.show();

*(a.p) = 7788;

b.show();


return 0;

}


编译后执行输出:

[root@localhost09operator]# ./a.out

88

88