C++基础编程----6.2函数
来源:互联网 发布:使用波士顿矩阵实例 编辑:程序博客网 时间:2024/05/20 02:51
函数
1.引用返回的是左值,可以赋值
2.返回数组指针
int (*func(int i))[10];
int even[]={0,2,4,6,8};
int odd[]={1,3,5,7,9};
decltype(odd) *arrptr(int i) // decltype并不负责把数组类型转换成对应的指针类型,所以它的结果仍然是个数组 返回 数组指针
{
return i%2?odd:even;
}
3.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);
}
4.函数指针
// 比较两个string对象的长度
bool lengthCompare(const string &,const string &); // 函数声明
// pf指向一个函数,该函数参数是两个const string&,返回值为bool类型
bool (*pf)(const string &,const string &); // 函数指针 (*pf)必须加括号,否则就变成了返回bool*类型
pf = lengthCompare; 或者 pf =&lengthCompare; // 等价赋值 &可选
typedef bool Func(const string &,const string &);
void useBigger(cosnt string &,const string &,Func); // Func函数类型,形参中会自动转换为函数指针类型
类似于:void useBigger(cosnt string &,const string &,bool Func(const string &,const string &));
类似于:typedef decltype(lengthCompare) *FuncP2;
void useBigger(cosnt string &,const string &,FuncP2);
5.尾置返回类型
C++11中一种简化声明的方法,就是使用尾置返回类型。任何函数定义都能使用尾置返回类型。尾置返回类型都跟在形参列表的后面并以一个->符号开头,在本该返回类型的地方放置一个auto
// func接受一个int类型的实参,返回一个指针,该指针指向一个含有十个元素的数组
auto func(int i)->int (*)[10];
- 嵌入式C基础编程 函数
- C 编程基础——main函数
- C++基础编程----6.2函数
- 编程基础-------C语言函数返回二维数组的做法
- 编程基础——C语言printf()函数的用法
- c语言基础--编程基础
- 网络编程基础函数
- shell编程基础------函数
- 函数式编程基础
- c#socket编程基础
- linux C编程基础
- c#socket 编程基础
- pro*c编程基础
- 嵌入式C编程基础
- PROC*C编程基础
- c#socket编程基础
- linux c编程基础
- c编程基础
- junit测试进阶
- hadoop
- 高层小高6层住房 哪个更划算
- 四.左上下级联页面
- linux 系统服务/程序 启动运行 怎么样才能自动运行
- C++基础编程----6.2函数
- WebView的使用,android与JS混编
- 杭电2717Catch That Cow
- VS 通过 附加进程的方式来调试 C#服务程序
- android lint三部曲二:定制Android-Lint检查问题的现有规则
- android-左右滑动页面设计-仿微信滑动引导页面
- 10个重要的算法C语言实现源代码
- frame buffer编程--重新架构代码,实现动态效果
- 数据分析师必须掌握的完整知识结构