函数指针、std::bind()、λ表达式、using例程

函数指针

#include <functional>
#include <iostream>

std::function<int(void)> funcPtr;
int foo1(void);
int foo2(void);
int foo3(void);
int foo4(void);

int foo1(void) {
    funcPtr = foo2;
    std::cout << "foo1: " << std::endl;
    return 0;
}

int foo2(void) {
    funcPtr = foo3;
    std::cout << "foo2: " << std::endl;
    return 0;
}

int foo3(void) {
    funcPtr = foo4;
    std::cout << "foo3: " << std::endl;
    return 0;
}

int foo4(void) {
    funcPtr = foo1;
    std::cout << "foo4: " << std::endl;
    return 0;
}

int main() {
    funcPtr = foo1;
    funcPtr(); // 调用函数
    funcPtr();
    funcPtr();
    funcPtr();

    return 0;
}

std::bind绑定

#include <functional>
#include <iostream>

std::function<int(void)> funcPtr;
int foo1(void);
int foo2(void);
int foo3(void);
int foo4(void);

int foo1(void) {
    funcPtr = std::bind(foo2);
    std::cout << "foo1: " << std::endl;
    return 0;
}

int foo2(void) {
    funcPtr = std::bind(foo3);
    std::cout << "foo2: " << std::endl;
    return 0;
}

int foo3(void) {
    funcPtr = std::bind(foo4);
    std::cout << "foo3: " << std::endl;
    return 0;
}

int foo4(void) {
    funcPtr = std::bind(foo1);
    std::cout << "foo4: " << std::endl;
    return 0;
}

int main() {
    funcPtr = std::bind(foo1);
    funcPtr(); // 调用函数
    funcPtr();
    funcPtr();
    funcPtr();

    return 0;
}

λ表达式

#include <functional>
#include <iostream>

std::function<int(void)> funcPtr;
int foo1(void);
int foo2(void);
int foo3(void);
int foo4(void);

int foo1(void) {
    funcPtr = []() { return foo2(); };
    std::cout << "foo1: " << std::endl;
    return 0;
}

int foo2(void) {
    funcPtr = []() { return foo3(); };
    std::cout << "foo2: " << std::endl;
    return 0;
}

int foo3(void) {
    funcPtr = []() { return foo4(); };
    std::cout << "foo3: " << std::endl;
    return 0;
}

int foo4(void) {
    funcPtr = []() { return foo1(); };
    std::cout << "foo4: " << std::endl;
    return 0;
}

int main() {
    funcPtr = []() { return foo1(); };
    funcPtr(); // 调用函数
    funcPtr();
    funcPtr();
    funcPtr();

    return 0;
}

using

#include <iostream>

namespace action
{
    enum class RunStatus {
        RUNNING,
        SUCCESS,
        FAILURE
    };

    class Docking
    {
    public:
        Docking();
        ~Docking();

        void run();

    private:
        RunStatus enter();
        RunStatus findIrda();
        RunStatus backDocking();

        using FuncPtr = RunStatus (Docking::*)();

        FuncPtr funcPtr;
    };

    Docking::Docking() : funcPtr(&Docking::enter)
    {
        std::cout << "Docking initialized" << std::endl;
    }

    Docking::~Docking()
    {
    }

    void Docking::run()
    {
        (this->*funcPtr)();
    }

    RunStatus Docking::enter()
    {
        funcPtr = &Docking::findIrda;
        std::cout << "Enter!!!" << std::endl;
        return RunStatus::SUCCESS;
    }

    RunStatus Docking::findIrda()
    {
        funcPtr = &Docking::backDocking;
        std::cout << "Find Irda!!!" << std::endl;
        return RunStatus::SUCCESS;
    }

    RunStatus Docking::backDocking()
    {
        funcPtr = &Docking::enter;
        std::cout << "Back Docking!!!" << std::endl;
        return RunStatus::SUCCESS;
    }
}

int main()
{
    using namespace action;

    Docking x;
    x.run();
    x.run();
    x.run();

    return 0;
}
  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值