【C++】函数重载分析

  函数重载:您可以在相同的范围内对同一个函数名有多个定义。函数的定义必须根据参数列表中的参数和/或参数的数量而有所不同。您不能重载仅由返回类型不同的函数声明。

一、函数重载分析

(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++语言课程

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值