[进阶]C++:函数重载

定义重载函数

  • 重载函数是函数名相同但是参数列表不同的函数
    void Print(const int* a, const int* b){}
    void Print(const int* a, const size_t b){}

重载和const形参

  • const_cast 在重载函数中最有用
const string& shorterString(const string& s1, const string& s2)
{
    return s1.size() <= s2.size() ? s1 : s2;
}
string& shorterString(string& s1, string &s2)
{
    auto& r = shorterString(const_cast<const string&>(s1),
        const_cast<const string&>(s2));
    return const_cast<string&>(r);
}

首先它的实参强制转换为了const对象,然后调用了shorterString函数const版本,const版本返回const string的引用,我们再将其转换回string&,这显然是安全的

int lookup(int );
int lookup(const int );//重复声明
int lookup(int* );
int lookup(int* const);//重复声明

int lookup(int&); 
int lookup(const int&);//新函数,作用于常量
int lookup(int*);
int lookup(const int*);//新函数,作用于指向常量的指针

默认实参

  • 要注意的是,一旦某个形参被赋予了默认值,它后面的所有值都必须有默认值。
typedef string::size_type sz;
string screen(sz ht = 24, sz wid = 80, char backgrnd = 'sd');

int main()
{
    std::cout << "Hello World!\n";
    string window;
    window = screen();
    window = screen(, ,'?');//错误,只能省略尾部


}

内联函数和constexpr函数

  • 内联函数可避免函数调用的开销
    调用函数一般比等价表达式要慢
inline const string& 
shorterString(const string& s1, const string& s2)
{
    return s1.size() <= s2.size() ? s1 : s2;
}

constexpr函数

  • 函数的返回类型和所有形参的类型都是字面值类型,而且函数体中必须有且只有一个return语句
constexpr int new_sz() { return 42; }
constexpr int foo = new_sz();//正确
constexpr size_t scale(size_t cnt) { return new_sz() * cnt; }

int arr[scale(2)];//正确:scale(2)是常量表达式
int i = 2;
int a2[scale(i)];//错误,scale(i)不是常量表达式
  • constexpr 函数不一定返回常量表达式

调试帮助

  • 程序可以包括一些用于调试的代码,但是这些代码只在开发程序时使用,当应用程序编写完成准备发布的时候,要先屏蔽掉调试代码。这种方法需要两项预处理功能assertNDEBUG
  • assert 预处理宏

assert是一种预处理宏。行为有点向内联函数。assert宏使用一个表达式作为它的条件:

assert(expr)

可以直接使用不需要using声明。首先对表达式求值。如果表达式为假,assert输出信息,并终止程序 。如果为真,什么都不做

NDEBUG预处理变量

  • assert的行为依赖于一个名为NDEBUG的预处理变量状态。如果定义了NDEBUG,则assert什么都不会做。在文件前加入#define NDBUG
int main()
{
    std::cout << "Hello World!\n";
    cout << __FILE__//文件名
        << __LINE__ //行标
        << __TIME__//时间
        << __DATE__ << endl;//日期
}

函数指针

  • 函数指针指向的是函数而非对象。函数的类型由他们的返回值类型和形参类型共同决定。与函数名无关。
bool LengthCompare(const string&, const string&);
bool (*pf)(const string&, const string&);//未初始化

int main()
{
    pf = LengthCompare;//pf指向名为LengthCompare的函数
    pf = &LengthCompare;//等价的赋值语句
    bool b1 = pf("hello", "Hi");//调用lengthCompare函数
    bool b2 = (*pf)("hello", "Hi");//一个等价的调用
    bool b3 = LengthCompare("hello", "Hi");//另一个等价的调用
    std::cout << "Hello World!\n";
    
}

函数指针形参

void useBigger(const string& s1,const string& s2,
    bool pf(const string&, const string&));//自动转换成指向函数的指针
void useBigger(const string& s1, const string& s2,
    bool (*pf)(const string&, const string&));//显示的将形参定义成指向函数的指针

直接使用函数指针类型的显得冗长而繁琐,类型别名可以帮助我们简化使用过程。

//Fun和Func2是函数类型
typedef bool Func(const string&, const string&);
typedef decltype(LengthCompare) Func2;//等价的类型
//FuncP和FuncP2是只想函数的指针
typedef bool (*FuncP)(const string&, const string&);
typedef decltype(LengthCompare)* FuncP2;//等价类型

void useBigger(const string& s1,const string& s2,Func);//自动转化为函数的指针
void useBigger(const string& s1,const string& s2,FuncP2);//

返回指向函数的指针

  • 想要声明一个返回函数指针的函数,最简单的办法是使用类型别名:
using F=int(int*,int); //F是函数类型,不是指针
using PF=int(*)(int*,int);//PF是指针类型

PF f1(int);//正确,PF是指向函数的指针,f1返回指向函数的指针
F f1(int);//错误,F的函数类型,f1不能返回一个函数
F *f1(int);//正确,显示的指定返回类型是指向函数的指针

当然也可以直接声明f1:

int (*f1(int))(int*,int);

将auto和decltype用于函数指针类型

int up(const string&, const string&);
int sub(const string&, const string&);
decltype(up) *getFun(const string&);

参考:C++primer 第五版

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前言 把《C++ Primer》[https://book.douban.com/subject/25708312...
    尤汐Yogy阅读 9,541评论 1 51
  • 1、函数基础 典型的函数定义包括:返回类型、函数名、由0个或多个形参组成的列表以及函数体。 2、参数传递 形参初始...
    久伴必知情深阅读 502评论 0 0
  • #1.函数基础1.1 局部对象1.2 函数声明1.3 分离式编译 #2.参数传递2.1 传值参数2.2 传引用参数...
    MrDecoder阅读 622评论 0 1
  • 1. C++基础 大多数编程语言通过两种方式来进一步补充其基本特征1)赋予程序员自定义数据类型的权利,从而实现对语...
    王侦阅读 768评论 0 3
  • 一些情况下函数有个别形参不会被用到,此类形参通常不命名以表示在函数体内不会使用它。但是即便是这样,调用此函数的时候...
    修司敦阅读 341评论 0 0