zoukankan      html  css  js  c++  java
  • boost中function的简单应用(将function作为参数进行传递)

    今天开发代码的时候发现前人的代码很多的重复,看的极累,因此考虑优化合并,结果发现需要动用到函数参数方能使得逻辑上看起来简单清晰。想到了boost的应用,可以比较简单的实现函数参数的功能。以下为简单的实现代码:

    // testfunction_param.cpp : Defines the entry point for the console application.
    // 一个简单的函数参数的应用,即将类函数作为一个入参,由别的函数通过此入参调用此函数。
    // boost方法比较简单的解决了这个问题。
    
    #include "stdafx.h"
    #include <iostream>
    #include <boost/bind.hpp>
    #include <boost/function.hpp>
    
    class Foo {
    public:  
      typedef boost::function<int(int)> Boost_Fun;
      Boost_Fun fun_;
      int square(int a) {return a*a;}
      void test(int a, Boost_Fun f){
        std::cout<<"test("<<a<<")"<<f(a)<<std::endl;
      }
    };
    int _tmain(int argc, _TCHAR* argv[])
    {
      Foo foo;
      Foo::Boost_Fun f = boost::bind(&Foo::square, &foo, _1);
      foo.test(4, f);
        return 0;
    }
  • 相关阅读:
    移动端注意
    每日日报
    每日日报
    每日日报
    每日日报
    每日日报
    每日日报
    每日日报
    每日日报
    每日日报
  • 原文地址:https://www.cnblogs.com/luhouxiang/p/2638369.html
Copyright © 2011-2022 走看看