C++重载运算符和重载函数详解

嵌入式技术

1343人已加入

描述

​C++重载运算符和重载函数

C++的运算符重载在维基百科中的定义如下:

在计算机程序设计中,运算符重载(英语:operator overloading)是多态的一种。这里,运算符(比如+,=或==)被当作多态函数,它们的行为随着其参数类型的不同而不同。运算符并不一定总是符号。

运算符重载通常只是一种语法糖。它可以简单地通过函数调用来模拟:

a + b * c

在一个支持运算符重载的语言里,上面的写法要比下面的写法有效而简练:

add(a, multiply(b, c))

(假设运算符* 的优先级高于运算符 +)

当一种语言允许运算符在某种情况下被隐式调用的时候,运算符重载将不只提供写法上的方便。例如,Ruby中的to_s运算符就是如此,它返回一个对象的字符串表示。

C++中的函数重载在维基百科中的定义如下:

函数重载(英语:function overloading),是Ada、C++、C#、D和Java等编程语言中具有的一项特性,这项特性允许创建数项名称相同但输入输出类型或个数不同的子程序,它可以简单地称为一个单独功能可以执行多项任务的能力。

C++允许在同一个作用域中的某个函数或者运算符指定多个定义,这样的方式分别被称为函数重载和运算符重载。

重载声明是指的一个与之前已经在这个作用域内声明过的函数或者方法具有相同的名称的声明,但是他们的参数和定义并不完全相同。

当调用一个重载函数和重载运算发的时候,编译器会通过把使用的参数类型的定义中的参数类型进行比较,最终选定最合适的定义,选择最合适的重载函数或者重载运算符的过程,这样的过程成员为重载决策。

C++中的函数重载

在同一个作用域内,可以声明几个功能类似的同名函数,但是这些同名函数的参数(参数个数,参数类型,参数顺序)必须不同,同时不能仅仅通过返回类型的不同来重载函数。

接下来我们看一段函数重载的代码:

  1. #include

  2. #include

  3. #include

  4. #include

  5. #include

  6. #include

  7. #include

  8. #include

  9. #include

  10. #include

  11. #include

  12. #include

  13. #define R register

  14. #define LL longlong

  15. #define pi 3.141

  16. #define INF 1400000000

  17. usingnamespace std;


  18. class printData {

  19. public:

  20. void print(int number){

  21. cout << number <<"n";

  22. }

  23. void print(double number){

  24. cout << number <<"n";

  25. }

  26. void print(string s){

  27. cout << s <<"n";

  28. }

  29. };


  30. int main(){

  31. printData test;

  32. test.print(1);

  33. test.print(1.1);

  34. test.print("abcde");

  35. return0;

  36. }

通过上面的代码,即使函数名相同,单数传入的参数类型不同,那么程序调用的函数也不相同,程序的执行结果也不相同,这就是C++中的函数名重载。

C++中的运算符重载

在C++中额可以重载或重定义大多数的内置的运算符,这样我们就可以使用自定义类型的运算符。

重载运算符的函数通常是带有特殊函数名的函数,函数是由关键字operator和后面的运算符符合组成的,和普通函数相同,重载的运算符有一个返回类型和参数列表。

形式如下:

  1. Boxoperator+(constBox&);

重载加法运算是用于将两个Box类型的对象进行相加并最终返回Box类型的对象,大多数的重载运算符可以被定义为普通的非成员函数或者类内的成员函数,如果定义的函数是类的非成员函数,那么我们需要在每次执行函数的时候向函数内传递两个参数,形式如下所示:

  1. Boxoperator+(constBox&,constBox&);

下面的代码通过使用成员函数进行了运算符重载,此时,对象作为参数进行传递,代码如下所示:

  1. #include

  2. #include

  3. #include

  4. #include

  5. #include

  6. #include

  7. #include

  8. #include

  9. #include

  10. #include

  11. #include

  12. #include

  13. #define R register

  14. #define LL longlong

  15. #define pi 3.141

  16. #define INF 1400000000

  17. usingnamespace std;


  18. classBox{

  19. private:

  20. int length, breadth, height;

  21. public:

  22. int get_volume(){

  23. return length * breadth * height;

  24. }

  25. void set_length(int l){

  26. length = l;

  27. }

  28. void set_breadth(int b){

  29. breadth = b;

  30. }

  31. void set_height(int h){

  32. height = h;

  33. }

  34. Boxoperator+(Box& add_box){

  35. Box box;

  36. box.length = add_box.length + length, box.breadth = add_box.breadth + breadth, box.height = add_box.height + height;

  37. return box;

  38. }

  39. };


  40. int main(){

  41. Box box1, box2, box3;

  42. box1.set_length(1), box1.set_breadth(2), box1.set_height(3);

  43. box2.set_length(4), box2.set_breadth(5), box2.set_height(6);

  44. box3 = box1 + box2;

  45. cout <<"The volume of box1 is: "<< box1.get_volume()<<"n";

  46. cout <<"The volume of box2 is: "<< box2.get_volume()<<"n";

  47. cout <<"The volume of box3 is: "<< box3.get_volume()<<"n";

  48. return0;

  49. }

可重载运算符/不可重载运算符

下面列出C++中可重载和不可重载的运算符。

可重载运算符:

运算符类型运算符名称
算数运算符+ - * / %
关系运算符== != < > <= >=
逻辑运算符
单目运算符+ - * &
自增运算符++ --
位运算符
赋值运算符= += -= *= /= %= &=
空间申请和释放new delete new[] delete[]
其他() -> , []

不可重载运算符:

运算符名称运算符符号
成员访问运算符.
成员指针访问运算符.* ->*
域运算符::
长度运算符sizeof
条件运算符?:
预处理符号#

下面逐个对可重载的运算符进行演示。

一元运算符重载

一元运算符指的是只对一个操作数进行操作,下面是一元运算符的实例。

递增运算符(++),递减运算符(--)

负号(-)

逻辑非运算符(!)

通常一元运算符出现在操作的对象的左边,下面演示重载负号(-)一元运算符:

  1. #include

  2. #include

  3. #include

  4. #include

  5. #include

  6. #include

  7. #include

  8. #include

  9. #include

  10. #include

  11. #include

  12. #include

  13. #define R register

  14. #define LL longlong

  15. #define pi 3.141

  16. #define INF 1400000000

  17. usingnamespace std;


  18. classPoint{

  19. private:

  20. int x, y;

  21. public:

  22. Point(int x_x =0,int y_y =0){

  23. x = x_x, y = y_y;

  24. }

  25. Pointoperator-(){

  26. x =-x, y =-y;

  27. return*this;

  28. }

  29. voidShow_Point(){

  30. cout <<"("<< x <<","<< y <<")n";

  31. }

  32. };


  33. int main(){

  34. int x, y;

  35. cin >> x >> y;

  36. Point p(x, y);

  37. -p;

  38. p.Show_Point();

  39. return0;

  40. }

上面设计了一段求点关于原点对称的点的代码,重载了-运算符来直接对对象进行操作。

二元运算符重载

二元运算符主要需要两个参数,我们通常使用的加(+),减(-),乘(*),除(/)都是二元运算符。接下来我们尝试重载 + 运算符:

  1. #include

  2. #include

  3. #include

  4. #include

  5. #include

  6. #include

  7. #include

  8. #include

  9. #include

  10. #include

  11. #include

  12. #include

  13. #define R register

  14. #define LL longlong

  15. #define pi 3.141

  16. #define INF 1400000000

  17. usingnamespace std;


  18. classBox{

  19. private:

  20. int length, breadth, height;

  21. public:

  22. int get_volume(){

  23. return length * breadth * height;

  24. }

  25. void set_length(int l){

  26. length = l;

  27. }

  28. void set_breadth(int b){

  29. breadth = b;

  30. }

  31. void set_height(int h){

  32. height = h;

  33. }

  34. Boxoperator+(Box& add_box){

  35. Box box;

  36. box.length = add_box.length + length, box.breadth = add_box.breadth + breadth, box.height = add_box.height + height;

  37. return box;

  38. }

  39. };


  40. int main(){

  41. Box box1, box2, box3;

  42. box1.set_length(1), box1.set_breadth(2), box1.set_height(3);

  43. box2.set_length(4), box2.set_breadth(5), box2.set_height(6);

  44. box3 = box1 + box2;

  45. cout <<"The volume of box1 is: "<< box1.get_volume()<<"n";

  46. cout <<"The volume of box2 is: "<< box2.get_volume()<<"n";

  47. cout <<"The volume of box3 is: "<< box3.get_volume()<<"n";

  48. return0;

  49. }

接下来我们尝试将上面的代码进行改写,将运算符重载的函数以非成员函数的方式进行重载运算符:

  1. #include

  2. #include

  3. #include

  4. #include

  5. #include

  6. #include

  7. #include

  8. #include

  9. #include

  10. #include

  11. #include

  12. #include

  13. #define R register

  14. #define LL longlong

  15. #define pi 3.141

  16. #define INF 1400000000

  17. usingnamespace std;


  18. classBox{

  19. private:

  20. int length, breadth, height;

  21. public:

  22. int get_volume(){

  23. return length * breadth * height;

  24. }

  25. void set_length(int l){

  26. length = l;

  27. }

  28. void set_breadth(int b){

  29. breadth = b;

  30. }

  31. void set_height(int h){

  32. height = h;

  33. }

  34. friendBoxoperator+(Box& add_box1,Box& add_box2);

  35. };

  36. Boxoperator+(Box& add_box1,Box&add_box2){

  37. Box box;

  38. box.length = add_box1.length + add_box2.length, box.breadth = add_box1.breadth + add_box2.breadth, box.height = add_box1.height + add_box2.height;

  39. return box;

  40. }


  41. int main(){

  42. Box box1, box2, box3;

  43. box1.set_length(1), box1.set_breadth(2), box1.set_height(3);

  44. box2.set_length(4), box2.set_breadth(5), box2.set_height(6);

  45. box3 = box1 + box2;

  46. cout <<"The volume of box1 is: "<< box1.get_volume()<<"n";

  47. cout <<"The volume of box2 is: "<< box2.get_volume()<<"n";

  48. cout <<"The volume of box3 is: "<< box3.get_volume()<<"n";

  49. return0;

  50. }

将重载函数作为非成员函数的时候,是将两个对象进行相加,并且由于函数是全局函数,那么需要传入的参数个数为2,同时当重载预算符的函数是全局函数的时候,需要在类中将这个函数声明为友元函数。

关系运算符重载

C++支持很多种关系运算符,这些关系运算符用来比较C++中的数据类型,我们同时也可以重载任何一个关系运算符,接下来我们尝试重载>,<,==关系运算符,从而实现一个成绩排名的小程序:

  1. #include

  2. #include

  3. #include

  4. #include

  5. #include

  6. #include

  7. #include

  8. #include

  9. #include

  10. #include

  11. #include

  12. #include

  13. #define R register

  14. #define LL longlong

  15. #define pi 3.141

  16. #define INF 1400000000

  17. usingnamespace std;


  18. classStudent{

  19. private:

  20. string name, ID;

  21. int score1, score2;

  22. public:

  23. Student(string n, string id,int number1,int number2){

  24. name = n, ID = id, score1 = number1, score2 = number2;

  25. }

  26. Student(){


  27. }

  28. booloperator>(Student& s){

  29. if(score1 + score2 > s.score1 + s.score2){

  30. returntrue;

  31. }

  32. else{

  33. returnfalse;

  34. }

  35. }

  36. booloperator<(Student& s){

  37. if(score1 + score2 < s.score1 + s.score2){

  38. returntrue;

  39. }

  40. else{

  41. returnfalse;

  42. }

  43. }

  44. booloperator==(Student& s){

  45. if(score1 + score2 == s.score1 + s.score2){

  46. returntrue;

  47. }

  48. else{

  49. returnfalse;

  50. }

  51. }

  52. };


  53. int main(){

  54. string name, ID;

  55. int score1, score2;

  56. cin >> name >> ID >> score1 >> score2;

  57. StudentStudent1(name, ID, score1, score2);

  58. cin >> name >> ID >> score1 >> score2;

  59. StudentStudent2(name, ID, score1, score2);

  60. if(Student1>Student2){

  61. cout <<"Student1 is better than Student2";

  62. }

  63. elseif(Student1==Student2){

  64. cout <<"Student1 is equal to Student2";

  65. }

  66. elseif(Student1<Student2){

  67. cout <<"Student1 is worse than Student2";

  68. }

  69. return0;

  70. }

输入输出运算符重载

C++能够使用流提取运算符>>和流插入运算符<<来进行输入和输出内置的数据类型,我们也可以重载流提取和插入运算符来操作对象或者我们自定义的数据类型。

通常我们会将输入术后出的函数声明为类的友元函数,这样我们在使用的函数就不需要创建对象二直接调用函数。

下面我们将之前的对学生成绩的代码进行修改,添加了重载流提取运算符和流插入运算符的部分:

  1. #include

  2. #include

  3. #include

  4. #include

  5. #include

  6. #include

  7. #include

  8. #include

  9. #include

  10. #include

  11. #include

  12. #include

  13. #define R register

  14. #define LL longlong

  15. #define pi 3.141

  16. #define INF 1400000000

  17. usingnamespace std;


  18. classStudent{

  19. private:

  20. string name, ID;

  21. int score1, score2;

  22. public:

  23. Student(string n, string id,int number1,int number2){

  24. name = n, ID = id, score1 = number1, score2 = number2;

  25. }

  26. Student(){


  27. }

  28. booloperator>(Student& s){

  29. if(score1 + score2 > s.score1 + s.score2){

  30. returntrue;

  31. }

  32. else{

  33. returnfalse;

  34. }

  35. }

  36. booloperator<(Student& s){

  37. if(score1 + score2 < s.score1 + s.score2){

  38. returntrue;

  39. }

  40. else{

  41. returnfalse;

  42. }

  43. }

  44. booloperator==(Student& s){

  45. if(score1 + score2 == s.score1 + s.score2){

  46. returntrue;

  47. }

  48. else{

  49. returnfalse;

  50. }

  51. }

  52. friend istream &operator>>(istream& input,Student&student){

  53. input >> student.name >> student.ID >> student.score1 >> student.score2;

  54. return input;

  55. }

  56. friend ostream &operator<<(ostream& output,Student&student){

  57. output <<"Student:"<< student.name <<" ID:"<< student.ID <<" total score:"<< student.score1 + student.score2 <<"n";

  58. return output;

  59. }

  60. };


  61. int main(){

  62. string name, ID;

  63. int score1, score2;

  64. cin >> name >> ID >> score1 >> score2;

  65. StudentStudent1(name, ID, score1, score2);

  66. cin >> name >> ID >> score1 >> score2;

  67. StudentStudent2(name, ID, score1, score2);

  68. if(Student1>Student2){

  69. cout <<"Student1 is better than Student2n";

  70. }

  71. elseif(Student1==Student2){

  72. cout <<"Student1 is equal to Student2n";

  73. }

  74. elseif(Student1<Student2){

  75. cout <<"Student1 is worse than Student2n";

  76. }

  77. cout <<Student1<<Student2;

  78. return0;

  79. }

上述代码我们将流插入和提取运算符进行重载,可以直接对对象中的成员变量进行输入和输出。

通常我们输入输出习惯上使用cin>>和cout<<,这样重载的时候需要使用友元函数来重载运算符,如果使用成员函数来重载运算符的时候将会出现d1<

++和--运算符重载

++和--运算符是C++中两个重要的一元运算符,下面的示例中以时间变化为例演示重载++运算符的前缀和后缀的两种用法:

  1. #include

  2. #include

  3. #include

  4. #include

  5. #include

  6. #include

  7. #include

  8. #include

  9. #include

  10. #include

  11. #include

  12. #include

  13. #define R register

  14. #define LL longlong

  15. #define pi 3.141

  16. #define INF 1400000000

  17. usingnamespace std;


  18. classTime{

  19. private:

  20. int hour, minute, second;

  21. public:

  22. Time(){

  23. hour =0, minute =0, second =0;

  24. }

  25. Time(int h,int m,int s){

  26. hour = h, minute = m, second = s;

  27. }

  28. friend ostream&operator<<(ostream& output,Time T){

  29. output <<"The time is "<< T.hour <<":"<< T.minute <<":"<< T.second <<"n";

  30. return output;

  31. }

  32. Timeoperator++(){

  33. ++second;

  34. if(second >=60){

  35. ++minute;

  36. minute -=60;

  37. }

  38. if(minute >=60){

  39. ++hour;

  40. minute -=60;

  41. }

  42. if(hour >=24){

  43. hour =0;

  44. }

  45. return*this;

  46. }

  47. Timeoperator++(int){

  48. ++second;

  49. if(second >=60){

  50. ++minute;

  51. minute -=60;

  52. }

  53. if(minute >=60){

  54. ++hour;

  55. minute -=60;

  56. }

  57. if(hour >=24){

  58. hour =0;

  59. }

  60. return*this;

  61. }

  62. };


  63. int main(){

  64. int hour, minute, second;

  65. cin >> hour >> minute >> second;

  66. Time T(hour, minute, second);

  67. ++T;

  68. cout << T;

  69. T++;

  70. cout << T;

  71. return0;

  72. }

递增和递减通常是改变对象的状态,所以对递增和递减运算符的重载通常为成员函数。

重载递增递减一定要和指针的递增和递减进行区分,疑问这里的重载操作的是对象而不是指针(由于指针是内置类型,因此指针的递增和递减是无法被重载的),所以通常情况下的递增和递减操作的对象是对象内部的成员变量。

递增和递减运算符分为前置和后置两种情况,因为符号相同,因此给后置的版本增加一个int类型的形参,这个形参为0,在函数体中式用不到的,引入这个形参只是为了区分富豪的前置和后置。

赋值运算符重载

和其他运算符相同,C++中允许我们重载赋值运算符,用来创建宇哥对象,比如拷贝构造函数。下面我们演示对赋值运算符的重载:

  1. #include

  2. #include

  3. #include

  4. #include

  5. #include

  6. #include

  7. #include

  8. #include

  9. #include

  10. #include

  11. #include

  12. #include

  13. #define R register

  14. #define LL longlong

  15. #define pi 3.141

  16. #define INF 1400000000

  17. usingnamespace std;


  18. classTime{

  19. private:

  20. int hour, minute, second;

  21. public:

  22. Time(){

  23. hour =0, minute =0, second =0;

  24. }

  25. Time(int h,int m,int s){

  26. hour = h, minute = m, second = s;

  27. }

  28. friend ostream&operator<<(ostream& output,Time T){

  29. output <<"The time is "<< T.hour <<":"<< T.minute <<":"<< T.second <<"n";

  30. return output;

  31. }

  32. Timeoperator=(Time& T){

  33. hour = T.hour, minute = T.minute, second = T.second;

  34. return*this;

  35. }

  36. };


  37. int main(){

  38. int hour, minute, second;

  39. cin >> hour >> minute >> second;

  40. Time T1(hour, minute, second);

  41. Time T2;

  42. T2 = T1;

  43. cout << T1 << T2;

  44. return0;

  45. }

上述代码通过将时间进行复制的操作演示了对赋值运算符的重载。

浅拷贝和隐式转换:通常情况我们使用隐式转换函数的时候十分谨慎,因为当我们不需要使用转换蛤属的时候,这个函数仍可能被调用执行,这些不正确的程序可能会出现一些意想不到的事情,而我们很难对此做出判断,同时浅拷贝存在指针悬空的问题,所以我们通常禁止隐式转换,在构造函数前面添加explicit关键字,通常情况下隐式转换的声明的意义不大,在实际情况中通常使用浅拷贝,不会调用隐式转换。

函数调用运算符重载

蛤属调用运算符可以被重载与类的对象,当我们重载()的时候,我们并不是调用了一种新的调用函数的方式,相反的这是创建了一个可以传递任意数目参数的运算符函数,下面我们通过求总秒数的例子来演示重载函数调用运算符:

  1. #include

  2. #include

  3. #include

  4. #include

  5. #include

  6. #include

  7. #include

  8. #include

  9. #include

  10. #include

  11. #include

  12. #include

  13. #define R register

  14. #define LL longlong

  15. #define pi 3.141

  16. #define INF 1400000000

  17. usingnamespace std;


  18. classTime{

  19. private:

  20. int hour, minute, second, total_second;

  21. public:

  22. Time(){

  23. hour =0, minute =0, second =0, total_second =0;

  24. }

  25. Time(int h,int m,int s){

  26. hour = h, minute = m, second = s, total_second =3600*24* hour +3600* minute + second;

  27. }

  28. void show_total_second(){

  29. cout << total_second;

  30. }

  31. Timeoperator()(int h,int m,int s){

  32. Time T;

  33. T.total_second =24*60*60* h +60*60* m + s;

  34. return T;

  35. }

  36. Timeoperator=(Time& T){

  37. this->total_second = T.total_second;

  38. return*this;

  39. }

  40. };


  41. int main(){

  42. int hour, minute, second;

  43. cin >> hour >> minute >> second;

  44. Time T1;

  45. Time T2 = T1(hour, minute, second);

  46. T2.show_total_second();

  47. return0;

  48. }

下标运算符[]重载

下标操作符[]通常用来访问数组元素,重载这个运算符可以增强操作数组的功能,下面的代码通过重载下标运算符来更方便得访问类中数组指定的元素:

  1. #include

  2. #include

  3. #include

  4. #include

  5. #include

  6. #include

  7. #include

  8. #include

  9. #include

  10. #include

  11. #include

  12. #include

  13. #define R register

  14. #define LL longlong

  15. #define pi 3.141

  16. #define INF 1400000000

  17. usingnamespace std;


  18. classArray{

  19. private:

  20. int number[10], posion;

  21. public:

  22. int&operator[](int i){

  23. return number[i];

  24. }

  25. Array(){

  26. posion =0;

  27. }

  28. void set_value(int num){

  29. number[posion]= num,++ posion;

  30. }

  31. };


  32. int main(){

  33. Array number;

  34. for(R int i =0; i <10;++i){

  35. int n;

  36. cin >> n;

  37. number.set_value(n);

  38. }

  39. for(R int i =0; i <10;++i){

  40. cout << number[i]<<" ";

  41. }

  42. return0;

  43. }

类成员访问运算符( -> )可以被重载,但它较为麻烦。它被定义用于为一个类赋予"指针"行为。运算符 -> 必须是一个成员函数。如果使用了 -> 运算符,返回类型必须是指针或者是类的对象。

运算符 -> 通常与指针引用运算符 * 结合使用,用于实现"智能指针"的功能。这些指针是行为与正常指针相似的对象,唯一不同的是,当通过指针访问对象时,它们会执行其他的任务。比如,当指针销毁时,或者当指针指向另一个对象时,会自动删除对象。

  1. #include

  2. #include

  3. #include

  4. #include

  5. #include

  6. #include

  7. #include

  8. #include

  9. #include

  10. #include

  11. #include

  12. #include

  13. #define R register

  14. #define LL longlong

  15. #define pi 3.141

  16. #define INF 1400000000

  17. usingnamespace std;


  18. // 假设一个实际的类

  19. classObj{

  20. staticint i, j;

  21. public:

  22. void f()const{ cout << i++<< endl;}

  23. void g()const{ cout << j++<< endl;}

  24. };


  25. // 静态成员定义

  26. intObj::i =10;

  27. intObj::j =12;


  28. // 为上面的类实现一个容器

  29. classObjContainer{

  30. vector<Obj*> a;

  31. public:

  32. void add(Obj* obj)

  33. {

  34. a.push_back(obj);// 调用向量的标准方法

  35. }

  36. friendclassSmartPointer;

  37. };


  38. // 实现智能指针,用于访问类 Obj 的成员

  39. classSmartPointer{

  40. ObjContainer oc;

  41. int index;

  42. public:

  43. SmartPointer(ObjContainer& objc)

  44. {

  45. oc = objc;

  46. index =0;

  47. }

  48. // 返回值表示列表结束

  49. booloperator++()// 前缀版本

  50. {

  51. if(index >= oc.a.size()-1)returnfalse;

  52. if(oc.a[++index]==0)returnfalse;

  53. returntrue;

  54. }

  55. booloperator++(int)// 后缀版本

  56. {

  57. returnoperator++();

  58. }

  59. // 重载运算符 ->

  60. Obj*operator->()const

  61. {

  62. if(!oc.a[index])

  63. {

  64. cout <<"Zero value";

  65. return(Obj*)0;

  66. }

  67. return oc.a[index];

  68. }

  69. };


  70. int main(){

  71. constint sz =10;

  72. Obj o[sz];

  73. ObjContainer oc;

  74. for(int i =0; i < sz; i++)

  75. {

  76. oc.add(&o[i]);

  77. }

  78. SmartPointer sp(oc);// 创建一个迭代器

  79. do{

  80. sp->f();// 智能指针调用

  81. sp->g();

  82. }while(sp++);

  83. return0;

  84. }

一些注意的要点

运算符重载不可以改变语法结构;

运算符重载不可以改变操作数的个数;

运算符重载不可以改变运算的优先级;

运算符重载不可以改变结合性。

类重载、覆盖、重定义之间的区别:

重载指的是函数具有的不同的参数列表,而函数名相同的函数。重载要求参数列表必须不同,比如参数的类型不同、参数的个数不同、参数的顺序不同。如果仅仅是函数的返回值不同是没办法重载的,因为重载要求参数列表必须不同。(发生在同一个类里)

覆盖是存在类中,子类重写从基类继承过来的函数。被重写的函数不能是static的。必须是virtual的。但是函数名、返回值、参数列表都必须和基类相同(发生在基类和子类)

重定义也叫做隐藏,子类重新定义父类中有相同名称的非虚函数 ( 参数列表可以不同 ) 。(发生在基类和子类)

this 指针的作用

this 指针是一个隐含于每一个非静态成员函数中的特殊指针。它指向正在被该成员函数操作的那个对象。当对一个对象调用成员函数时,编译器先将对象的地址赋给 this 指针,然后调用成员函数,每次成员函数存取数据成员时由隐含使用 this 指针。

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分