qt开发环境 - c++函数重载与解析

#include <QCoreApplication>
#include <iostream>

using namespace std;

#define unused(a) a=a
void foo(void){
    cout << 1 << endl;
}

void foo(int n){
    unused(n);
    cout << 2 << endl;
}

void foo(int* p){
    unused(p);
    cout << 3 <<endl;
}

int foo(double x){
    unused(x);
    cout << 4 << endl;
    return 0;
}

char const* foo(double x ,int y){
    unused(x);unused(y);
    cout << 5 << endl;
    return NULL;
}

char const* foo(int x, double y){
    unused(x);unused(y);
    cout << 6 << endl;
    return NULL;
}

void bar(char a){
    unused(a);
    cout << 'A' << endl;
}

void bar(int a){
    unused(a);
    cout << 'B' << endl;
}
void bar(long long a){
    unused(a);
    cout << 'C' << endl;
}
void hum(char* p, int n){
    unused(p);unused(n);
    cout << "aa" << endl;
}

void hum(char const* p, char n){
    unused(p);unused(n);
    cout << "bb" << endl;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    int n;
    double d = 0;
    //函数名字相同,参数表不同,构成重载,重载和返回值、参数名、相同类型的引用和非引用也不构成重载
    //根据参数表类型来寻找寒素
    foo();//1
    foo(n);//2
    foo(&n);//3
    foo(d);//4
    foo(n,d);//6
    foo(d,n);//5
    //如果不是像前面那样完美匹配,编译器会选择最安全的那个,工作量最小的
    short m = 0;
    bar(m);//B
    //被警告,编译器说这俩都不好,它选择的第二个,第二个更安全,不需要扩展内存
    char* p = NULL;
    char u = 0;
    hum(p, u);
    //作用域,名字空间嵌套,最近的 
    return a.exec();
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值