码迷,mamicode.com
首页 > 编程语言 > 详细

c++11 常用语法

时间:2017-05-16 17:28:33      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:c++   getc   hello   end   c++11   getchar   调用   类成员   返回   

1.using用法

typedef void (*ptr)(int);

using ptr = void (*)(int);

 

2.可调用对象

1.函数指针

2.具有operator()成员函数的对象(仿函数)

3.可被转换为函数指针的类对象

4.类成员(函数)指针

举例:

1.void func(void){}

2.class foo {

  void operator()(void){}

}

3.class bar {

  using ptr = void (*)(void);

  static void func(void){}

  operator ptr(){ return func; }

}

4.

class a {
public:
int a_;

void mem_func(void)
{
cout << "hello function" << endl;
}

};

int main()
{
void(a::*ptr)(void) = &a::mem_func;
int a::*ptr_num = &a::a_;
a aa;
(aa.*ptr)();
aa.*ptr_num = 100;

getchar();
return 0;
}

 

 

2.lambda

[capture](params)opt->ret{body;}

capture:捕获列表 空、&、=、this

params:参数表

opt:函数选项

ret:返回值

body:函数体

完整的lambda表达式

auto f = [](int a) -> int{ return a+1; }

auto f = [](int a){ return a+1; }

 

实例:

//typedef int (*ptr)(int i);
using ptr = int (*)(int i);
int main()
{
//ptr f = [](int a){ return a + 100; };
//std::function<int(int)> f = [](int a){ return a + 1; };
//std::function<int(int)> f = std::bind([](int a){ return a / 10; }, std::placeholders::_1);
std::function<int(int)> f = func;
cout << f(100) << endl;
getchar();
return 0;
}

c++11 常用语法

标签:c++   getc   hello   end   c++11   getchar   调用   类成员   返回   

原文地址:http://www.cnblogs.com/kaishan1990/p/6862249.html

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