本文介绍了功能对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 考虑以下代码, #include< iostream> struct Functor { void operator()(void){std :: cout<< "函子" <<的std :: ENDL; } }; int main(无效) { Functor(); 返回0; } 为什么运营商()不需要是否创建了临时对象? - anderberg:人们只是突变。有些比其他人差。 解决方案 为什么要这样? 尝试Functor()()。 - 答:因为它弄乱了人们通常阅读文本的顺序。 /> 问:为什么这么糟糕? A:热门发布。 问:usenet上最烦人的事情是什么?电子邮件? 因为你实际上并没有*打电话给它。 Functor() 只是创建一个临时对象。现在,添加()到它,你得到 Functor()() 将导致_call_。 然后,从括号内删除那些''void'。他们就像疮。 唉! V 喜欢明白。 另外,应该是''const''。 当然,这不是关于代码''正确性'的问题。 只是一个简单的例子来说明我的问题。 没有必要''main'',因为''main''是一个特殊功能。 见上文。 Functor a; a(); 我认为Functor()会转换成类似于 $的东西b $ b以上。我想我错了。但问题仍然存在 - 为什么 不是吗? 尝试Functor()()。 谢谢! - anderberg:人们只是突变。有些人比其他人差。 Consider the following code,#include <iostream>struct Functor {void operator()(void) { std::cout << "Functor" << std::endl; }};intmain(void){Functor();return 0;}Why isn''t operator() called for the temporary object that''s created?--anderberg: People are just mutations. Some worse than others. 解决方案Why should it?Try Functor()().--A: Because it messes up the order in which people normally read text.Q: Why is it such a bad thing?A: Top-posting.Q: What is the most annoying thing on usenet and in e-mail?Because you didn''t actually *call* it.Functor()is just creation of a temporary object. Now, add () to it and you getFunctor()()which will result in a _call_.And, drop those ''void'' from inside the parentheses. They''re like sores.Ugh!VLike to be explicit. Also, should probably be ''const''.Of course, this wasn''t a question about the code''s "correctness".Just a simple case of illustrating my question. Not necessary in ''main'', because ''main'' is a special function.See above. Why should it?Functor a;a();I figured that Functor() would translate to something similar tothe above. I guess I was wrong. But the question remains - whydoesn''t it? Try Functor()().Thanks!--anderberg: People are just mutations. Some worse than others. 这篇关于功能对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-30 08:20