Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6087486
  • 博文数量: 2759
  • 博客积分: 1021
  • 博客等级: 中士
  • 技术积分: 4091
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-11 14:14
文章分类

全部博文(2759)

文章存档

2019年(1)

2017年(84)

2016年(196)

2015年(204)

2014年(636)

2013年(1176)

2012年(463)

分类: C/C++

2013-09-11 12:06:23

原文地址:C++函数升级(二) 作者:特殊借口

一、函数重载(function Overload)
1>
用同一个函数名定义不同的函数;

当函数名和不同的参数搭配时函数的含义不同。

例1:

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <string.h>

  3. int func(int x)
  4. {
  5.     return x;
  6. }

  7. int func(int a, int b)
  8. {
  9.     return a + b;
  10. }

  11. int func(const char* s)
  12. {
  13.     return strlen(s);
  14. }

  15. int main(int argc, char *argv[])
  16. {
  17.     int c = 0;
  18.     
  19.     c = func(1);
  20.     
  21.     printf("c = %d\n", c);
  22.     
  23.     c = func(1, 2);
  24.     
  25.     printf("c = %d\n", c);
  26.     
  27.     c = func("12345");
  28.     
  29.     printf("c = %d\n", c);
  30.     
  31.     printf("Press enter to continue ...");
  32.     getchar();    
  33.     return 0;
  34. }



函数重载至少满足下面一个条件:


1、参数个数不同;
2、参数类型不同;
3、参数顺序不同;
例2:


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <string.h>

  3. int func(int x)
  4. {
  5.     return x;
  6. }

  7. int func(int a, int b)
  8. {
  9.     return a + b;
  10. }

  11. int func(const char* s)
  12. {
  13.     return strlen(s);
  14. }

  15. int func(int a, const char* s)
  16. {
  17.     return a;
  18. }

  19. int func(const char* s, int a)
  20. {
  21.     return strlen(s);
  22. }

  23. int main(int argc, char *argv[])
  24. {
  25.     int c = 0;
  26.     
  27.     c = func("ab", 1);
  28.     
  29.     printf("c = %d\n", c);
  30.     
  31.     printf("Press enter to continue ...");
  32.     getchar();    
  33.     return 0;
  34. }


当函数默认参数遇上函数重载的时候,存在二义性,调用失败,编译不能通过。
例3:

点击(此处)折叠或打开

  1. include <stdio.h>
  2. #include <string.h>

  3. int func(int a, int b, int c = 0)
  4. {
  5.     return a * b * c;
  6. }

  7. int func(int a, int b)
  8. {
  9.     return a + b;
  10. }

  11. int main(int argc, char *argv[])
  12. {
  13.     int c = 0;
  14.     
  15.     c = func(1, 2); // 存在二义性,调用失败,编译不能通过
  16.     
  17.     printf("c = %d\n", c);
  18.     
  19.     printf("Press enter to continue ...");
  20.     getchar();    
  21.     return 0;
  22. }



2> 译器调用重载函数的准则
1、将所有同名函数作为候选者;
2、尝试寻找可行的候选函数;
精确匹配实参,通过默认参数能够匹配实参,通过默认类型转换匹配实参。

匹配失败:
最终寻找到的可行候选函数不唯一,则出现二义性,编译失败。
无法匹配所有候选者,函数未定义,编译失败。


3>函数重载的注意事项
1、重载函数在本质上是相互独立的不同函数
2、重载函数的函数类型是不同的
3、函数返回值不能作为函数重载的依据

函数重载是由函数名和参数列表决定的。

4>函数重载与函数指针
例4:


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <string.h>

  3. int func(int x) // int(int a)
  4. {
  5.     return x;
  6. }

  7. int func(int a, int b)
  8. {
  9.     return a + b;
  10. }

  11. int func(const char* s)
  12. {
  13.     return strlen(s);
  14. }

  15. typedef int(*PFUNC)(int a); // int(int a)

  16. int main(int argc, char *argv[])
  17. {
  18.     int c = 0;
  19.     PFUNC p = func;
  20.     
  21.     c = p(1);
  22.     
  23.     printf("c = %d\n", c);
  24.     
  25.     printf("Press enter to continue ...");
  26.     getchar();    
  27.     return 0;
  28. }


当使用重载函数名对函数指针进行赋值时
1、根据重载规则挑选函数指针参数列表一致的候选者
2、严格匹配候选者的函数类型与函数指针的函数类型

二、C++和C的相互调用


在项目中融合C++和C代码是实际工程中不可避免的,虽然C++编译器能够兼容C语言的编译方式,但C++编译器会优先使用C++的方式进行编译;利用 extern 关键字强制让C++编译器对代码进行C方式编译。

实例1:C++调用C编写的函数


点击(此处)折叠或打开

  1. /*add.h*/

  2. int add(int a, int b)


点击(此处)折叠或打开

  1. /*add.c*/
  2. #include "add.h"

  3. int add(int a, int b)
  4. {
  5.     return a + b;
  6. }


点击(此处)折叠或打开

  1. /*main.cpp*/
  2. #include <stdio.h>

  3. extern "C"
  4. {
  5. #include "add.h"
  6. }


  7. int main()
  8. {
  9.     printf("1 + 2 = %d\n", add(1, 2));
  10.     
  11.     return 0;
  12. }
gcc -c add.c

gcc  main.cpp add.o

./a,out


实例2:C调用C++编写的函数


点击(此处)折叠或打开

  1. /*add.h*/
  2. int add(int a, int b);


点击(此处)折叠或打开

  1. /*add.cpp*/


  2. extern "C"
  3. {

  4. #include "add.h"

  5. int add(int a, int b)
  6. {
  7.     return a + b;
  8. }

  9. }


点击(此处)折叠或打开

  1. /*main.c*/

  2. #include <stdio.h>
  3. #include "add.h"

  4. int main()
  5. {
  6.     printf("1 + 2 = %d\n", add(1, 2));
  7.     
  8.     return 0;
  9. }

g++ -c add.cpp

gcc main.c -lstdc++ add.o

./a.out

统一的解决方案
__cplusplus 是C++编译器内置的标准宏定义,其意义是让C代码即可以通过C编译,也可以在C++编译器中以C方式编译。

例5:


点击(此处)折叠或打开

  1. #ifdef __cplusplus
  2. extern "C" {
  3. #endif



  4. #ifdef __cplusplus
  5. }
  6. #endif


注意:C++编译器不能以C的方式编译多个重载函数

例6:


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <string.h>


  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif

  6. int func(int a, int b)
  7. {
  8.     return a + b;
  9. }

  10. int func(const char* s)
  11. {
  12.     return strlen(s);
  13. }

  14. #ifdef __cplusplus
  15. }
  16. #endif

  17. int main(int argc, char *argv[])
  18. {
  19.     printf("Press enter to continue ...");
  20.     getchar();    
  21.     return 0;
  22. }


小结:函数重载是C++对C语言的一个重要升级,函数重载通过函数参数列表区分不同的同名函数,函数的返回值类型不是函数重载的依据;extern 关键字能够实现C和C++的相互调用。所以C和C++并不对立,可以同时存在于项目中!
阅读(400) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~