码迷,mamicode.com
首页 > 其他好文 > 详细

函数重载

时间:2016-08-19 00:40:27      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

重点:

1.重载函数:同一作用域内的几个函数名字相同但形参列表不同。

NOTE: main函数不能重载。

2.两个函数如果只有返回类型不同,是不可以的。

3.Typedef  A  B  BA的别名。

4.顶层const没有区分:&const , *const

  底层const 可区分:const& const*

5.只能把const对象(或指向const的指针)传递给const形参;

  相反,非常量可以转换成const

6.是否重载函数要看哪个更容易理解。

7.函数的参数和返回类型都是const string的引用,可以两个非常量   的string实参调用这个函数,返回结果仍然是string const&

  Const string &shorterString ( const string &s1 , const string &s2)

  {

      Return s1.size() <= s2.size() ? S1 : s2;

  }

  当它的实参不是常量时,得到的结果  是一个普通的引用,使用c  onst_cast实现:

  String &shorterString ( string &s1 , string &s2 )

 {

     Auto &r = shorterString ( const_cast<const string&>(s1),

                            Const_cast<const string&>(s2));

     Return const_cast <string&> (r);

}

8.函数匹配是指一个过程,在这个过程中我们把函数调用与一组重载函数中的某一个关联起来,函数匹配又叫做重载确定。

9.如果我们在内层作用域中声明名字,它将隐藏外层作用域中的声明的同名实体。

//不同的作用域无法重载函数名

String read();

Void print(const string &);

Void print(double);//重载print函数

Void fooBar(int ival)

{
   bool read = false;//新作用域,隐藏了外层的read

   Strings = read();//错误:read是一个布尔值,而不是一个函数;

 //不好的习惯:通常来说,在局部作用域当中声明函数不是一个好的选择;

   Void print(int);//新作用域,隐藏了之前的print

   Print(Value:);//错误:print(const string &)被隐藏了;

   Print(ival);//正确:当前的printint可见;

   Print(3,14);//正确:3.14 -> 3 调用printint);

}

10.C++语言中,名字查找发生在类型检查之前。

  调用某个函数时,编译器首先寻找对函数的声明,一旦在当前作用域中找到了所需的名字,编译器就会忽略掉外层的同名实体,剩下的就是检查函数调用是否有效了。

函数重载

标签:

原文地址:http://www.cnblogs.com/YH-shjd-senvn/p/5785836.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!