C++ const castmember function的解读。在线等

std::function转载自:/w/cpp/utility/functional/function
std::function Defined in header template& class & /* undefined */(since C++11)template& class R, class... Args &class function&R(Args...)&(since C++11)Class template std::function is a general-purpose polymorphic function wrapper. Instances of std::function can store, copy, and invoke any
target -- functions, , bind expressions, or other function objects, as well as pointers to member functions and pointers to data members.The stored callable object is called the target of std::function. If a std::function contains no target, it is calledempty. Invoking the target of an empty std::function results in
exception being thrown.std::function satisfies the requirements of
and .Member typesTypeDefinitionresult_typeRargument_typeT if sizeof...(Args)==1 and T is the first and only type in Args...first_argument_typeT1 if sizeof...(Args)==2 and T1 is the first of the two types in Args...second_argument_typeT2 if sizeof...(Args)==2 and T2 is the second of the two types in Args...Member functionsconstructs a new std::function instance (public member function)destroys a std::function instance (public member function)assigns a new target (public member function)swaps the contents (public member function)assigns a new target (public member function)operator boolchecks if a valid target is contained (public member function)invokes the target (public member function)Target accessobtains the typeid of the stored target of a std::function (public member function)obtains a pointer to the stored target of a std::function (public member function)Non-member functions(C++11)specializes the
algorithm (function template)compares an std::function with std::nullptr (function template)Helper classes(C++11)specializes the
type trait (class template specialization)ExampleRun this code#include &functional&
#include &iostream&
struct Foo {
Foo(int num) : num_(num) {}
void print_add(int i) const {
&& num_+i && '\n'; }
void print_num(int i)
&& i && '\n';
struct PrintNum {
void operator()(int i) const
&& i && '\n';
int main()
// store a free function
std::function&void(int)& f_display = print_
f_display(-9);
// store a lambda
std::function&void()& f_display_42 = []() { print_num(42); };
f_display_42();
// store the result of a call to std::bind
std::function&void()& f_display_31337 = (print_num, 31337);
f_display_31337();
// store a call to a member function
std::function&void(const Foo&, int)& f_add_display = &Foo::print_
const Foo foo(314159);
f_add_display(foo, 1);
// store a call to a member function and object
using std::placeholders::_1;
std::function&void(int)& f_add_display2= ( &Foo::print_add, foo, _1 );
f_add_display2(2);
// store a call to a member function and object ptr
std::function&void(int)& f_add_display3= ( &Foo::print_add, &foo, _1 );
f_add_display3(3);
// store a call to a function object
std::function&void(int)& f_display_obj = PrintNum();
f_display_obj(18);
}Output:-9
如果您想留下此文,您可以将其发送至您的邮箱(将同时以邮件内容&PDF形式发送)
相关文章推荐
(Ctrl+Enter提交) &&
已有0人在此发表见解
&在& 11:34收藏到了
&&在信息爆炸的时代,您的知识需要整理,沉淀,积累!Lai18为您提供一个简单实用的文章整理收藏工具,在这里您可以收藏对您有用的技术文章,自由分门别类,在整理的过程中,用心梳理自己的知识!相信,用不了多久,您收藏整理的文章将是您一生的知识宝库!
· 蜀ICP备号-1您所在位置: &
&nbsp&&nbsp&nbsp&&nbsp
华工C++课后习题及其解答(第3版)解读.doc 131页
本文档一共被下载:
次 ,您可全文免费在线阅读后下载本文档。
下载提示
1.本站不保证该用户上传的文档完整性,不预览、不比对内容而直接下载产生的反悔问题本站不予受理。
2.该文档所得收入(下载+内容+预览三)归上传者、原创者。
3.登录后可充值,立即自动返金币,充值渠道很便利
需要金币:350 &&
课后习题及其解答(第3版)解读
你可能关注的文档:
··········
··········
习题1及其解答1.1选择题1.一个最简单的C++程序,可以只有一个(c)。(a)库函数
(b)自定义函数
(c)main函数
(d)空函数2.用C++语言编制的源程序要变为目标程序必须要经过(d)。(a)解释
(d)编译3.C++程序中的简单语句必须以(b)结束。(a)冒号
(d)花括号4.有说明inta=0;doublex=5.16;以下语句中,(c)属于编译错误。(a)x=a/x;
(d)x=x*a;5.执行C++程序时出现的“溢出”错误属于(c)错误。(a)编译 (b)连接 (c)运行 (d)逻辑6.下列选项中,全部都是C++关键字的选项为(c)。(a)whileIFStatic
(b)breakchargo(c)sizeofcaseextern
(d)switchfloatinteger7.按C++标识符的语法规定,合法的标识符是(a)。(a)_abc
(d)“age“8.C++语句中,两个标识符之间(a)不能作为C++的分隔符。(a)数字
(d)+9.下列正确的八进制整型常量表示是(b)。(a)0a0
(d)0x1010.下列错误的十六进制整型常量表示是(c)。(a)0x11
(d)0x1f11.在下列选项中,全部都合法的浮点型数据的选项为(b)。(a)-1e3.515.2e-4
(b)12.34-1e+50.1E-12(c)0.2e-2-12345.e-5
(d)5.0e(1+4)0.18e+212.下列正确的字符常量为(d)。(a)“a”
(b)‘name’
(d)‘\101’13.下列选项中,(d)不能交换变量a和b的值。(a)t=b;b=a;a=t;
(b)a=a+b;b=a-b;a=a–b;(c)t=a;a=b;b=t;
(d)a=b;b=a;14.关于下列语句叙述错误的是(a)。inti=10,*p=&i;(a)p的值为10
(b)p指向整型变量i(c)*p表示变量i的值
(d)p的值是变量i的地址15.有以下变量说明,下面不正确的赋值语句是(b)。inta=5,b=10,c;int*p1=&a,*p2=&b;(a)*p2=b;
(b)p1=a;(c)p2=p1;
(d)c=*p1*(*p2);16.有以下变量说明,下面正确的语句是(b)。inta=10,b;int&pa=a,&pb=b;(a)&pb=a;
(d)*pb=*17.执行下面语句序列后,a和b的值分别为(b)。inta=5,b=3,t;int&ra=a;int&rb=b;t=ra=rb=t;(a)3和3
(d)5和518.在下列运算符中,(d)优先级最高。(a)&=
(d)*19.在下列运算符中,(d)优先级最低。(a)!
(d)?:20.设inti=1,j=2;则表达式i+++j的值为(c)。(a)1
(d)421.设inti=1,j=2;则表达式++i+j的值为(d)。(a)1
(d)422.在下列表达式选项中,(c)是正确。(a)++(a++)
(d)a++++b23.已知inti=0,j=1,k=2;则逻辑表达式++i||--j&&++k的值为(b)。(a)0
(d)324.执行下列语句后,x的值是(d),y的值是(c)。intx,y;x=y=1;++x||++y;(a)不确定
(d)225.设为整型变量,能正确表达1<x<5的逻辑表达式是()。(a)1&&5
(b)x==2||x==3||x==4(c)1&x&&x&5
(d)!(x&=1)&&!(x&=5)26.已知intx=5;执行下列语句后,x的值为(c)。x+=x-=x*x;(a)25
(d)2027.设inta=1,b=2,c=3,d=4;则以下条件表达式的值为(a
正在加载中,请稍后...您要找的页面不存在
您要找的页面不存在
可能是因为您的链接地址有误、该文章已经被作者删除或转为私密状态。该问题被发起重新开启投票
投票剩余时间:
之前被关闭原因:
该问题被发起删除投票
投票剩余时间:
距离悬赏到期还有:
参与关闭投票者:
关闭原因:
该问题已经被锁定
锁定原因:()
保护原因:避免来自新用户不合宜或无意义的致谢、跟帖答案。
该问题已成功删除,仅对您可见,其他人不能够查看。
const成员函数表示这个成员函数不会修改对象的成员,是针对对象的。该函数含有this指针。函数调用方式为thiscall。
而static是表示不属于任何一个对象,是全局的,调用规约是__cdecl或__stdcall。
static函数不能用const修饰的原因:一个静态成员函数访问的值是其参数、静态数据成员和全局变量,而这些数据都不是对象状态的一部分。而对成员函数中使用关键字const是表明:函数不会修改该函数访问的目标对象的数据成员。既然一个静态成员函数根本不访问非静态数据成员,那么就没必要使用const了。
因为我们在定义一个类对象的时候,实际上只给该对象的非静态的数据成员分配内存空间(假设没有虚函数),而该类的静态成员数据以及该类的函数都在编译的时候分配到一个公共的空间里,所有,在定义一个对象并调用类对象的函数的时候,函数根本不知道到底是哪个对象调用了他,怎么解决这个问题呢?C++利用传递this指针的方式来实现,调用一个类对象里的函数的时候,将把这个对象的指针传递给他,以便函数对该对象的数据进行操作,对于一个定义为const的函数,传递的是const的this指针,说明不能更改对象的属性,而对static成员的函数不需传递this指针,所有就不需要用const来修饰static的成员函数了!就说const属性的作用就是对被传递的this指针加以限定,而对static成员函数的调用根本不传递this指针,所有不需const来修饰static的成员函数。从对象模型上来说,类的非static成员函数在编译的时候都会扩展加上一个this参数,const的成员函数被要求不能修改this所指向的这个对象;而static函数编译的时候并不扩充加上this参数,自然无所谓const。 因为const成员函数的const是修饰this所指之物的,也就是this将会被声明为const CLASSNAME*。而static不存在this指针,所以const对static member function的修饰是无效的。 static member怎么看都是活在CLASSNAME NAMESPACE中的普通事物。
static成员函数没有this指针,而const成员函数中的const是用来修饰this指针的。
如果一个类的成员函数是const, 你会发现在这个函数中不能调用非const成员函数(虽然这个函数可能行为上符合const函数), 因为编译器是完美主义者, 他假设非const成员函数一定会修改this中的数据, 编译时就会报错, 所以所类的const成员函数表示的意思是: 我保证不会修改this的数据, 而static成员函数是属于类共同所有的, 它没有this, 所以staic和const同时修饰成员函数是非法的
不是您所需,查看更多相关问题与答案
德问是一个专业的编程问答社区,请
后再提交答案
关注该问题的人
共被浏览 (4127) 次普通函数可以用const声明吗_c++吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:282,033贴子:
普通函数可以用const声明吗收藏
普通函数可以用const声明吗
c++海同强大的师资阵容,因人制定课程内容,分阶段学习.c++就到正规IT技术培训机构-海同科技,培训IT技术面对面教学,免费重读!
不可以,只有在类中的非静态函数才可以使用const限定符
:c++11 9.3.2 [class.this] 1.The type of this in a member function ofa class X is X*. If the member function is declared const, the type of this is const X*, if the member function is declared volatile, the type of this is volatile X*, and if the member function is declared const volatile, the type of this is const volatile X*.
好像没我的事了。
登录百度帐号推荐应用
为兴趣而生,贴吧更懂你。或}

我要回帖

更多关于 const cast 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信