当前位置:   article > 正文

C++函数重载与const_c++ 函数重载 const

c++ 函数重载 const


前言

利用const也可以实现函数重载,但也是在一定情况下才能

一、参数只有有无const的区别

1.利用const函数重载的例子

#include <iostream>
#include <string.h>
using namespace std;
class A{
	public:
		int fun(const int &a)const{
			cout<<"fun:const"<<endl;
			return 0;
		}
		int fun(int &a){
			cout<<"fun:int"<<endl;
			return 0;
		}
};
int f(const int &a){
	cout<<"f:const"<<endl;
	return 0;
}
int f(int &a){
	cout<<"f:int"<<endl;
	return 0;
}
int main(){
	const int a1=1;
	int a2=2;
	f(a1);
	f(a2);
	A asd;
	asd.fun(a1);
	asd.fun(a2);
	return 0;
}
//或者参数不用引用,用指针,也是一样的效果例:int f(const int *a)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

输出如下:

f:const
f:int
fun:const
fun:int

2.利用const不能函数重载的例子

#include <iostream>
#include <string.h>
using namespace std;
class A{
	public:
		int fun(const int a)const{//没有第二个const则报错
			cout<<"fun:const"<<endl;
			return 0;
		}
		int fun(int a){
			cout<<"fun:int"<<endl;
			return 0;
		}
};
/*int f(const int a){
	cout<<"f:const"<<endl;
	return 0;
}
int f(int a){
	cout<<"f:int"<<endl;
	return 0;
}*/编译不能通过
int main(){
	const int a1=1;
	int a2=2;
//	f(a1);
//	f(a2);
	A asd;
	asd.fun(a1);
	asd.fun(a2);
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

输出如下:

fun:int
fun:int

分析:
在类外定义的函数,若参数不是传址,则编译报错;
在类内定义的函数,若参数不是传址,则只调用无const的函数;
在类外和类内定义的函数,若参数是传址,重载规则如下:
1.const重载主要是通过能否对传入的参数进行修改为判断的。
2.const参数重载和const函数重载机制(const函数重载机制即,在函数后加const)都是一样的,因为对于const 函数重载可看做是对隐含的指针this的参数重载。

二、其他情况

以下情况几乎用不着

1.情况1

#include <iostream>
#include <string.h>
using namespace std;
class A{
	public:
		int fun(const int &a)const{//去掉第二个const,则和函数f一样,调用也会报错
			cout<<"fun:const"<<endl;
			return 0;
		}
		int fun(int a){
			cout<<"fun:int"<<endl;
			return 0;
		}
};
int f(const int &a){
	cout<<"f:const"<<endl;
	return 0;
}
int f(int a){
	cout<<"f:int"<<endl;
	return 0;
}
int main(){
	const int a1=1;
	int a2=2;
//	f(a1);//error
//	f(a2);//error
	A asd;
	asd.fun(a1);
	asd.fun(a2);
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

输出如下:

fun:int
fun:int

虽然如上所述重载函数编译不会出错,但是一调用就会出错,因为重载的两个函数,均不能修改传入的参数,不能判断调用哪一个。

2.情况2

#include <iostream>
#include <string.h>
using namespace std;
class A{
	public:
		int fun(const int a)const{//去掉第二个const,则和函数f一样,调用asd.fun(a2)也会报错
			cout<<"fun:const"<<endl;
			return 0;
		}
		int fun(int &a){
			cout<<"fun:int"<<endl;
			return 0;
		}
};
int f(const int a){
	cout<<"f:const"<<endl;
	return 0;
}
int f(int &a){
	cout<<"f:int"<<endl;
	return 0;
}
int main(){
	const int a1=1;
	int a2=2;
	f(a1);
//	f(a2);//error
	A asd;
	asd.fun(a1);
	asd.fun(a2);
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

输出如下:

f:const
fun:const
fun:int

3.情况3

和情况2类似

#include <iostream>
#include <string.h>
using namespace std;
class A{
	public:
		int fun(int a)const{//去掉第二个const,则和函数f一样,调用asd.fun(a2)也会报错
			cout<<"fun:const"<<endl;
			return 0;
		}
		int fun(int &a){
			cout<<"fun:int"<<endl;
			return 0;
		}
};
int f(int a){
	cout<<"f:const"<<endl;
	return 0;
}
int f(int &a){
	cout<<"f:int"<<endl;
	return 0;
}
int main(){
	const int a1=1;
	int a2=2;
	f(a1);
//	f(a2);//error
	A asd;
	asd.fun(a1);
	asd.fun(a2);
	return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

输出如下:

f:const
fun:const
fun:int


总结

在类外定义的函数,若参数不是传址,则编译报错;
在类内定义的函数,若参数不是传址,则只调用无const的函数;
在类外和类内定义的函数,若参数是传址,重载规则如下:
1.const重载主要是通过能否对传入的参数进行修改为判断的。
2.const参数重载和const函数重载机制(const函数重载机制即,在函数后加const)都是一样的,因为对于const 函数重载可看做是对隐含的指针this的参数重载。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/838303
推荐阅读
  

闽ICP备14008679号