20140801--function指针(c++11新标准)

1.头文件:#include .用c++11的标准编译时要用 $g++ -o filename filename.cpp -std=c++0x
2.我们平时用的指针是这样的:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

void test(int i, double j){
    cout << "i = " << i << ", j = " << j << endl;
}

int main(int argc, const char *argv[])
{
    void (*p)(int, double) = test; //OK
    //void (*p)(int, double) = &test; //OK
    //void (*p)(int, double);  p = &test; //OK
    // void (*p)(int, double); p = test; //OK
    p(3, 3.2);
    return 0;
}

当我们用function定义指针的时候可以这样用:

#include <iostream>
#include <vector>
#include <string>
#include <functional>

using namespace std;


void test(int i, double j){
    cout << "i = " << i << ", j = " << j << endl;
}

int main(int argc, const char *argv[])
{
    //函数是void (*)(int, double) 类型的
    function<void (int, double)> fp;
    fp = test;
    fp(10, 3.24);
    return 0;
}

接下来搞一个升级版本:

#include <iostream>
#include <string>
#include <vector>
#include <functional>

using namespace std;

// void (*)()类型
void test(){
    cout << "test..." << endl;
}

class Test{
    public:
        // void (*)()类型
        static void test_static(){
            cout << "in Test test_static." << endl;
        }
        //void (Test::*)()类型
        void test2(){
            cout << "in Test test2." << endl;
        }
        // void (Test::*)(int) 类型
        void test3(int i){
            cout << "in Test test3. i = " << i << endl;
        }

};

int main(int argc, const char *argv[])
{
    function<void ()> fp;
    fp = test;
    fp();//相当于调用test()
    
    fp = Test::test_static;
    fp();//这个也OK

    Test t;
    fp = bind(&Test::test2, &t);
    fp();
    //这个也是OK的,因为function<>尖括号中是我们希望的fp的类型
    //如果fp实际中不是这个类型的,比如上面这个例子,
    //Test::test2()有一个隐式的参数this 我们通过bind绑定好,
    //这样在调用fp的时候就可以不必传入this这个参数,
    //看上去fp就好像是void (*)()类型一样
    
    function<void (int)> fp2;
    fp2 = bind(&Test::test3, &t, std::placeholders::_1);
    fp2(12345);
    //将fp2与t绑定,std::placeholders::_1 称为占位符,
    //_1表示原函数中的第一个形式参数,这里没有进行对第一个参数的绑定
    //只是占了一个位置

    return 0;
}

再来一个升级版,换一下形参位置

#include <iostream>
#include <string>
#include <vector>
#include <functional>
using namespace std;

void test(int i, double d, const string &s){
    cout << "i = " << i << ", d = " << d << ", s = " << s << endl;
}

int main(int argc, const char *argv[])
{
    using namespace std::placeholders;
    //原test函数是void (*)(int, double, const string &)类型

    //1.void (*)(int, double)
    function<void (int, double)> fp;
    string s1 = "-christmas-";
    fp = bind(&test,
              _1,
              _2,
              s1);
    fp(123, 100.234);

    //2.void (*)(double, int, const string &)
    function<void (double, int, const string&)> fp2;
    fp2 = bind(&test,
               _2,//表示test的第一个参数放在fp2的第二个位置上
               _1,//表示test的第二个参数放在fp2的第一个位置上
               _3);//表示test的第三个参数放在fp2的第三个位置上
    fp2(1.2, 100, "foobar");

    
    //3.void (*)(const string &, int) 跟2同理
    function<void (int, const string&)> fp3;
    fp3 = bind(&test,
              _1, 
              2.321,
              _2);
    fp3(100, "dream");

    //4.void (*)(const string &, int, double)f
    function<void (const string&, int, double)> fp4;
    fp4 = bind(&test,
               _2,
               _3,
               _1);
    fp4("foobar", 200, 23.3);


    //5.void (*)(int)
    function<void (int)> fp5;
    fp5 = bind(&test,
               _1,
               32.342,
               "detectiveh");

    fp5(200);

    //6. void (*)(const string*)
    function<void (const string &)> fp6;
    fp6 = bind(&test,
               90,
               23.543,
               _1);
    fp6("evitcetedh");


    //7. void (*)()
    function<void ()>fp7;
    fp7 = bind(&test, 100, 21.234, "hwy");
    fp7();

    return 0;
}

posted on 2014-08-01 20:48  detectiveh  阅读(148)  评论(0编辑  收藏  举报

导航