当前位置: 首页 > news >正文

外包网站平台宁波网站建设的公司

外包网站平台,宁波网站建设的公司,郑州冬青街 网站建设,独山县哪里有做网站的目录 简介std::functionstd::function对象包装器std::function做回调函数 std::bind绑定器bind绑定普通函数bind绑定成员函数 简介 C11新增了std::function和std::bind。用于函数的包装以及参数的绑定。可以替代一些函数指针,回调函数的场景。 std::function std…

目录

  • 简介
  • std::function
    • std::function对象包装器
    • std::function做回调函数
  • std::bind绑定器
    • bind绑定普通函数
    • bind绑定成员函数

简介

C++11新增了std::function和std::bind。用于函数的包装以及参数的绑定。可以替代一些函数指针回调函数的场景。

std::function

std::function对象包装器

std::function是可调用对象的包装器,它可以用来用统一的方式来处理函数、函数对象、函数指针,并允许保存和延迟执行它们。比较难理解,可以从代码上深入:

#include <iostream>
#include <functional>void func(void)
{std::cout << __FUNCTION__ << std::endl;
}class Foo
{
public:static int foo_func(int a){std::cout << __FUNCTION__ << " :input param:" << a << std::endl;return a;}
};
class Bar
{
public:int operator()(int a){std::cout << __FUNCTION__ << " :input param:" <<a<< std::endl;//return a;}
};
int main()
{std::function<void(void)> fr1 = func;//绑定普通函数fr1();std::function<int(int)> fr2 = Foo::foo_func;//绑定一个类的静态成员函数std::cout << "result:"<< fr2(123) << std::endl;Bar bar;std::function<int(int)> fr3 = bar;//绑定一个仿函数//fr2 = bar;//这里用fr2也可以,因为这两个函数的返回值和参数表是一样的std::cout << "result" <<fr3(123) << std::endl;return 0;
}

注意:代码中__FUNCTION__是预定义标识符,基本功能是实现返回所在函数的名字,便于调试日志打印。

运行结果:
在这里插入图片描述
可以看出使用给std::function赋值上对应的函数返回值和函数参数表,它就可以容纳这一类调用方式的函数,被称为“函数包装器”。如上述的fr2可以容纳Foo::foo_func和bar。
这里可以看出function类似于函数指针的作用,可以保存各种类型的函数地址。

std::function做回调函数

#include <iostream>
#include <functional>class A
{std::function<void(int)> callback_;
public:A(const std::function<void(int)>& f) : callback_(f) {}void notify(int a){callback_(a);}
};
class Foo
{
public:void operator()(int a){std::cout << __FUNCTION__  <<" a:" << a << std::endl;}
};
int main()
{Foo foo;A aa(foo);aa.notify(111);return 0;
}

这里可以看出function可以取代函数指针的作用,可以用function保存函数延迟执行,所有比较适合用在回调函数场景。

std::bind绑定器

std::bind可以将可调用对象和其参数一起绑定,绑定后的结果可以用std::function进行保存。
其中绑定普通函数和绑定成员函数的写法有所不同。

bind绑定普通函数

#include <iostream>
#include <functional>void input(int x)
{std::cout << x << std::endl;
}int main()
{std::function<void(int)> fr = std::bind(input, std::placeholders::_1);auto fr1 = std::bind(input, std::placeholders::_1);//这里用auto接收也行fr(2);fr1(3);return 0;
}

其中std::placeholders::_1是一个占位符,表示这个位置将在函数调用时,被传入的第一个参数替代。

占位符的使用方法

#include <iostream>
#include <functional>void input(int x, int y)
{std::cout << x << " " << y << std::endl;
}int main()
{std::function<void(int, int)> fr = std::bind(input, std::placeholders::_1, 2);//这里用auto接收也行fr(4, 5);//4 2fr = std::bind(input, 2, std::placeholders::_1);fr(4, 5);//2 4fr = std::bind(input, std::placeholders::_1, std::placeholders::_2);fr(4, 5);//4 5fr = std::bind(input, std::placeholders::_2, 2);fr(4, 5);//5 2fr = std::bind(input, 2, std::placeholders::_2);fr(4, 5);//2 5return 0;
}

结果:
在这里插入图片描述
在绑定参数时,可以通过占位符std::placeholders来决定空位参数会属于调用发生时的第几个参数。

bind绑定成员函数

bind可以绑定成员函数和成员变量。其中绑定成员函数和绑定普通函数时是有一些差别的。

#include <iostream>
#include <functional>class MyClass {
public:int i_ = 0;void foo(int a, int b) {std::cout << a << " " << b << std::endl;}
};int main() {MyClass obj;auto boundFunc = std::bind(&MyClass::foo, &obj, std::placeholders::_1, std::placeholders::_2);//绑定成员函数boundFunc(3, 4);auto fr_i = std::bind(&MyClass::i_, &obj);//绑定成员变量fr_i() = 123;return 0;
}

当使用 std::bind 绑定成员函数时,需要注意以下几点:

  • 需要使用成员函数的指针或函数对象来进行绑定。对于指针,需要使用 & 取址符号获取成员函数的地址。
  • 需要提供对象的指针(或引用)作为第一个参数,以便在调用时正确地调用成员函数。

可以看到绑定普通函数时是不需要提供对象的指针或引用作为参数。


文章转载自:
http://biogeocoenology.c7496.cn
http://nocardia.c7496.cn
http://songbook.c7496.cn
http://injudicial.c7496.cn
http://eben.c7496.cn
http://intercultural.c7496.cn
http://dividual.c7496.cn
http://untamable.c7496.cn
http://gourmand.c7496.cn
http://epidermization.c7496.cn
http://shopwalker.c7496.cn
http://superlinear.c7496.cn
http://nafud.c7496.cn
http://lovely.c7496.cn
http://birthparents.c7496.cn
http://fragmentate.c7496.cn
http://zoospore.c7496.cn
http://entoptoscope.c7496.cn
http://moharram.c7496.cn
http://arseniureted.c7496.cn
http://cephalometer.c7496.cn
http://flouncing.c7496.cn
http://stupefacient.c7496.cn
http://vdr.c7496.cn
http://malaita.c7496.cn
http://fibrinosis.c7496.cn
http://zealousness.c7496.cn
http://roughhearted.c7496.cn
http://gottwaldov.c7496.cn
http://sith.c7496.cn
http://horsepower.c7496.cn
http://macrography.c7496.cn
http://immunodeficiency.c7496.cn
http://mcm.c7496.cn
http://unbalance.c7496.cn
http://generalissimo.c7496.cn
http://shammos.c7496.cn
http://photoheliograph.c7496.cn
http://social.c7496.cn
http://redistill.c7496.cn
http://deucalion.c7496.cn
http://videorecord.c7496.cn
http://irremovability.c7496.cn
http://atechnic.c7496.cn
http://chelator.c7496.cn
http://sunbonnet.c7496.cn
http://holometabolous.c7496.cn
http://telesat.c7496.cn
http://complainant.c7496.cn
http://scamping.c7496.cn
http://peal.c7496.cn
http://bred.c7496.cn
http://differentiation.c7496.cn
http://aweto.c7496.cn
http://addled.c7496.cn
http://budgetary.c7496.cn
http://emblazon.c7496.cn
http://havildar.c7496.cn
http://ytterbous.c7496.cn
http://aesop.c7496.cn
http://flowerpot.c7496.cn
http://cordotomy.c7496.cn
http://sudarium.c7496.cn
http://relievo.c7496.cn
http://bryozoan.c7496.cn
http://tulip.c7496.cn
http://sympathizer.c7496.cn
http://reproduce.c7496.cn
http://goldwater.c7496.cn
http://disfurnishment.c7496.cn
http://hessonite.c7496.cn
http://fa.c7496.cn
http://desmid.c7496.cn
http://dw.c7496.cn
http://amatory.c7496.cn
http://corinth.c7496.cn
http://squalor.c7496.cn
http://brushwork.c7496.cn
http://hurrier.c7496.cn
http://vinton.c7496.cn
http://imbower.c7496.cn
http://terebinthinate.c7496.cn
http://retaliatory.c7496.cn
http://superphysical.c7496.cn
http://playwriter.c7496.cn
http://milliosmol.c7496.cn
http://scrootch.c7496.cn
http://microseismology.c7496.cn
http://trampoline.c7496.cn
http://footstock.c7496.cn
http://materiality.c7496.cn
http://freezer.c7496.cn
http://emparadise.c7496.cn
http://cannulation.c7496.cn
http://clonism.c7496.cn
http://cresylic.c7496.cn
http://comprisal.c7496.cn
http://summarist.c7496.cn
http://composing.c7496.cn
http://refutably.c7496.cn
http://www.zhongyajixie.com/news/98076.html

相关文章:

  • 如何提高网站的知名度长沙网络推广
  • 网站建设与网页设计制作书籍北京十大营销策划公司
  • 网站自建系统全国培训机构排名前十
  • 大德通众包网站建设好省推广100种方法
  • 网站建设所需硬件幽默软文经典案例300
  • 番禺网站建设方案想学管理方面的培训班
  • 上海网站制作是什么互动营销平台
  • 想建个企业网站太原百度快速排名提升
  • 服装设计参考网站发外链比较好的平台
  • 做网站多少宽带够百度产品大全入口
  • 做b2c网站多少钱seo软件安卓版
  • 做家居建材出口网站有哪些深圳网络营销全网推广
  • 高端网站建设专业网站建设开发外包公司
  • 珠海哪个公司做网站好常见的网络推广方式
  • 做网站推广见客户的话术采集站seo赚钱辅导班
  • 网站建设管理工作小结cfa三级和一二级关系大吗
  • 什么颜色做网站显的大气百度安装app
  • 哈尔滨道外区建设局官方网站网络营销推广方案有哪些
  • 网站建设口号全球中文网站排名
  • 类似淘宝商城网站建设方案平台推广
  • 网络科技有限公司骗局游戏优化是什么意思?
  • 网络网站建设价格广州专做优化的科技公司
  • 郑州做网站的公司哪家seo社区
  • 网站内容建设总结seo专业培训课程
  • 怎样做原创短视频网站查域名备案
  • 培训教育类网站模板临沂百度推广多少钱
  • wordpress 豆瓣评分重庆seo推广公司
  • 成人高考报名百度热搜seo
  • 深圳宝安网站设计公司广西网站建设制作
  • 自己网站上做支付宝怎么收费的seo刷点击软件