版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
1.函数重载回顾
- 函数重载的本质为相互独立的不同函数
- C++中通过函数名和函数参数确定函数调用
- 无法直接通过函数名得到重载函数的入口地址
- 函数重载必然发生在同—个作用域中
2.类中的重载
- 类中的成员函数可以进行重载
- 构造函数的重载
- 普通成员函数的重载
- 静态成员函数的重载
- 问题: 全局函数,普通成员函数以及静态成员函数之间是否可以构成重载?
3.万变不离其宗
- 1. 重载函数的本质为多个不同的函数
- 2. 函数名和参数列表是唯—的标识
- 3. 函数重载必须发生在同一个作用域中
-
#include<stdio.h>
-
-
class Test
-
{
-
int i;
-
-
public:
-
Test()
-
{
-
printf(
"Test::Test()\n");
-
this->i =
0;
-
}
-
-
Test(
int i)
-
{
-
printf(
"Test::Test(int i)\n");
-
this->i = i;
-
}
-
-
Test(
const Test &obj)
-
{
-
printf(
"Test(const Test &obj)\n");
-
this->i = obj.i;
-
}
-
-
static void func()
-
{
-
printf(
"void Test::func()\n");
-
}
-
-
void func(int i)
-
{
-
printf(
"void Test::func(int i),i = %d\n", i);
-
}
-
-
int getI()
-
{
-
return i;
-
}
-
};
-
-
void func()
-
{
-
printf(
"void func()\n");
-
}
-
-
void func(int i)
-
{
-
printf(
"void func(int i),i = %d\n", i);
-
}
-
-
int main()
-
{
-
func();
//全局函数的重载
-
func(
1);
//通过参数列表
-
-
Test t;
//成员函数的重载
-
Test t1(1);
-
Test t2(t1);
-
-
func();
//全局函数和静态成员函数之间的重载,注意作用域
-
Test::func();
-
-
func(
2);
//全局函数与普通成员函数之间的重载,注意作用域
-
t1.func(
2);
-
t1.func();
-
-
getchar();
-
-
return
0;
-
}
- 运行结果
4.重载的意义:
- 通过函数名对函数功能进行提示
- 通过参数列表对函数用法进行提示
- 扩展系统中已经存在的函数功能
-
#include <stdio.h>
-
#include <string.h>
-
-
char *strcpy(char *buf, const char * str, unsigned int n)
-
{
//重构原有函数
-
return
strncpy(buf, str, n);
-
}
-
-
int main()
-
{
-
const
char *s =
"Hello World";
-
char buf[
16] = {
0 };
-
-
strcpy(buf, s,
sizeof(buf) -
1);
-
-
printf(
"%s\n", buf);
-
-
getchar();
-
-
-
return
0;
-
}
- 打印结果为所给字符串。
6.小结
- 类的成员函数之间可以进行重载
- 重载必须发生在同一个作用域中
- 全局函数和成员函数不能构成重载关系(不在同一个作用域中)
- 重载的意义在于扩展已经存在的功能
【C++】函数重载分析
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
函数重载:您可以在相同的范围内对同一个函数名有多个定义。函数的定义必须根据参数列表中的参数和/或参数的数量而有所不同。您不能重载仅由返回类型不同的函数声明。
一、函数重载分析
(1)特点
1.同一个函数名定义不同的函数。 |
2.当函数名和不同的参数搭配时函数的定义不同 |
example :
-
#include <stdio.h>
-
#include <string.h>
-
#include <iostream>
-
-
int func(int x)
-
{
-
return x;
-
}
-
-
int func(int a, int b)
-
{
-
return a + b;
-
}
-
-
int func(const char* s)
-
{
-
return
strlen(s);
-
}
-
-
-
int main(int argc, char *argv[])
-
{
-
printf(
"%d\n", func(
3));
-
printf(
"%d\n", func(
4,
5));
-
printf(
"%d\n", func(
"D.T.Software"));
-
-
system(
"pause");
-
return
0;
-
}
-
-
编译运行:
可见,函数名相同,参数类型和参数个数不同,系统会调用不同的函数。
(2)函数重载至少满足下面的一个条件
1.参数个数不同 |
2.参数类型不同 |
3.参数顺序不同 |
example :
-
-
int
func(int a, const char* s)
-
{
-
return a;
-
}
-
-
int
func(const char* s, int a)
-
{
-
return strlen(s);
-
}
上面的两个函数也是重载函数。
(3)当函数默认参数遇上函数重载会发生什么?
example:
-
#include <stdio.h>
-
-
int
func(int a, int b, int c = 0)
-
{
-
return a * b * c;
-
}
-
-
int
func(int a, int b)
-
{
-
return a + b;
-
}
-
-
-
int main(
int argc, char *argv[])
-
{
-
int c =
func(1, 2);
-
-
return 0;
-
}
-
-
编译运行:
可见,编译器不知道调用哪个函数,从而产生二义性。
(4)函数重载的注意事项
1.重载函数在本质上是相互独立的不同函数 |
2.重载函数的函数类型不同 |
3.函数返回值不能作为函数重载的依据 |
note;函数重载由函数名和参数列表决定的。
example:
-
#include <stdio.h>
-
#include <iostream>
-
-
int add(
int a,
int b) //
int(
int,
int)
-
{
-
return a + b;
-
}
-
-
int add(
int a,
int b,
int c) //
int(
int,
int,
int)
-
{
-
return a + b + c;
-
}
-
-
int main()
-
{
-
printf(
"%p\n", (
int(*)(
int,
int))add);
-
printf(
"%p\n", (
int(*)(
int,
int,
int))add);
-
-
system(
"pause");
-
return
0;
-
}
编译运行:
两个重载函数打印的地址不一样,说明两个函数的编译入口不同。
二、重载与指针
(1)函数重载遇上函数指针
将重载函数名赋值给函数指针时:
1.根据重载规则挑选与函数指针列表一致的候选者 | |
2.严格匹配候选者的函数与函数指针的函数类型 |
Test:
下面的函数指针将保存哪个函数的地址?
-
#include <stdio.h>
-
#include <string.h>
-
#include <iostream>
-
-
int func(int x)
-
{
-
return x;
-
}
-
-
int func(int a, int b)
-
{
-
return a + b;
-
}
-
-
int func(const char* s)
-
{
-
return
strlen(s);
-
}
-
-
typedef int(*PFUNC)(int a);
-
-
-
int main(int argc, char *argv[])
-
{
-
int c =
0;
-
-
PFUNC p = func;
-
-
c = p(
1);
-
-
printf(
"c = %d\n", c);
-
-
system(
"pause");
-
return
0;
-
}
-
编译运行:
说明调用的是函数:
int func(int x)
{
return x;
}
因为typedef int(*PFUNC)(int a); 的参数类型是int ,与之匹配。
(2)注意:
1.函数重载必然发生在同一个作用域中 |
2.编译器需要用参数列表或函数类型进行函数选择 |
3.无法直接通过函数名得到重载函数的入口地址 |
Example:
-
#include <stdio.h>
-
#include <iostream>
-
-
int add(int a, int b)
-
{
-
return a + b;
-
}
-
-
int add(int a, int b, int c)
-
{
-
return a + b + c;
-
}
-
-
-
int main(int argc, char *argv[])
-
{
-
-
-
printf(
"%p\n", add);
-
-
printf(
"%p\n", add);
-
system(
"pause");
-
return
0;
-
}
-
编译运行:
说明无法直接通过函数名得到重载函数的入口地址。
我们将函数改成函数指针:
-
#include <stdio.h>
-
#include <iostream>
-
-
int add(int a, int b)
-
{
-
return a + b;
-
}
-
-
int add(int a, int b, int c)
-
{
-
return a + b + c;
-
}
-
-
-
int main(int argc, char *argv[])
-
{
-
printf(
"%p\n", (
int(*)(
int,
int))add);
-
-
printf(
"%p\n", (
int(*)(
int,
int,
int))add);
-
-
system(
"pause");
-
return
0;
-
}
-
编译运行,成功。
声明:
本文为听课笔记,课程为:唐佐林老师嵌入式c++语言课程