C++函数重载
// strtref.cpp -- using structure references
#include <iostream>
using namespace std;
void Fun(const int *num)
{
//num=435;
cout<< *num<<",const fun"<<endl;
}
void Fun(int *num)
{
//num=435;
cout<< *num<<",not const fun"<<endl;
}
int main()
{
const int num=999;
Fun(p);//输出 “999,const fun”
Fun(p2);//输出 “123,not const fun”
return 0;
#include <iostream>
using namespace std;
void Fun(const int *num)
{
//num=435;
cout<< *num<<",const fun"<<endl;
}
void Fun(int *num)
{
//num=435;
cout<< *num<<",not const fun"<<endl;
}
int main()
{
const int num=999;
const int *p=#
int avg=123;
int *p2=&avg;
Fun(p);//输出 “999,const fun”
Fun(p2);//输出 “123,not const fun”
return 0;
}
但是注意,非const实参也能匹配const形参,但是const实参不能匹配非const形参:
1.非const实参也能匹配const形参
#include <iostream>
using namespace std;
void Fun(const int *num)
{
//num=435;
cout<< *num<<",const fun"<<endl;
}
int main()
{
int avg=123;
int *p2=&avg;
Fun(p2);//输出“123,const fun”
return 0;
}
2.const实参不能匹配非const形参 ,下面这段代码编译不能通过,会报错“不能讲const int * 转换为 int*”
#include <iostream>
using namespace std;
void Fun( int *num)
{
//num=435;
cout<< *num<<",const fun"<<endl;
}
int main()
{
int avg=123;
const int *p2=&avg;
Fun(p2);
return 0;
}