• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

               C++ 技術中心

               :: 首頁 :: 聯系 ::  :: 管理
              160 Posts :: 0 Stories :: 87 Comments :: 0 Trackbacks

            公告

            鄭重聲明:本BLOG所發表的原創文章,作者保留一切權利。必須經過作者本人同意后方可轉載,并注名作者(天空)和出處(CppBlog.com)。作者Email:coder@luckcoder.com

            留言簿(27)

            搜索

            •  

            最新隨筆

            最新評論

            評論排行榜

            /* 
             * File:   main.cpp
             * Author: Vicky.H
             * Email:  eclipser@163.com
             
            */
            #include <iostream>
            #include <functional>
            #include <typeinfo>
            #include <string.h>

            int add1(int i, int j, int k) {
                return i + j + k;
            }


            class Utils {
            public:
                Utils(const char* name) {
                    strcpy(_name, name);
                }
                
                void sayHello(const char* name) const {
                    std::cout << _name << " say: hello " << name << std::endl;
                }
                
                static int getId() {
                    return 10001;
                } 
                
                int operator()(int i, int j, int k) const {
                    return i + j + k;
                }
                
            private:
                char _name[32];
            };


            /*
             * 
             
            */
            int main(void) {
                
                // 綁定全局函數
                auto add2 = std::bind(add1, std::placeholders::_1, std::placeholders::_2, 10);
                // 函數add2 = 綁定add1函數,參數1不變,參數2不變,參數3固定為10.
                std::cout << typeid(add2).name() << std::endl;
                std::cout << "add2(1,2) = " << add2(1, 2) << std::endl;
                
                std::cout << "\n---------------------------" << std::endl;
                
                // 綁定成員函數
                Utils utils("Vicky");
                auto sayHello = std::bind(&Utils::sayHello, utils/*調用者*/, std::placeholders::_1/*參數1*/);
                sayHello("Jack");
                
                auto sayHelloToLucy = std::bind(&Utils::sayHello, utils/*調用者*/, "Lucy"/*固定參數1*/);
                sayHelloToLucy();
                
                // 綁定靜態成員函數
                auto getId = std::bind(&Utils::getId);
                std::cout << getId() << std::endl;
                
                std::cout << "\n---------------------------" << std::endl;
                
                // 綁定operator函數
                auto add100 = std::bind(&Utils::operator (), utils, std::placeholders::_1, std::placeholders::_2, 100);
                std::cout << "add100(1, 2) = " << add100(1, 2) << std::endl;
                
                // 注意:無法使用std::bind()綁定一個重載函數
                
                return 0;
            }

            /* 
             * File:   main2.cpp
             * Author: Vicky.H
             * Email:  eclipser@163.com
             
            */
            #include <iostream>
            #include <typeinfo>


            void sayHello() {
                std::cout << "Hello world !" << std::endl;
            }

            int sum(int i, int j, int k) {
                return i + j + k;
            }

            template <typename T>
            class Func {
            public:

                Func(T fun) {
                    if (!fun) {
                        throw "fun nullptr";
                    }
                    _fun = fun;
                }

                template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5>
                R Call(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5) {
                    return _fun(a1, a2, a3, a4, a5);
                }

                template<typename R, typename A1, typename A2, typename A3, typename A4>
                R Call(A1 a1, A2 a2, A3 a3, A4 a4) {
                    return _fun(a1, a2, a3, a4);
                }

                template<typename R, typename A1, typename A2, typename A3>
                R Call(A1 a1, A2 a2, A3 a3) {
                    return _fun(a1, a2, a3);
                }

                template<typename R, typename A1, typename A2>
                R Call(A1 a1, A2 a2) {
                    return _fun(a1, a2);
                }

                template<typename R, typename A1>
                R Call(A1 a1) {
                    return _fun(a1);
                }

                template<typename R>
                R Call() {
                    return _fun();
                }

                void Call() {
                    _fun();
                }

            private:
                T _fun;
            };

            #include <functional>

            template<typename R = void, typename Args>
            class Fn {
            public:
                Fn(std::function<R(Args)> fun) : _fun(fun) {
                }
                
                R operator()(Args args) {
                    return _fun(args);
                }
            private:
                std::function<R(Args) > _fun;
            };

            /*
             * 將函數注冊到對象中,通過對象直接調用
             
            */
            int main(void) {


                Func<void(*)() > sayHelloFunc(sayHello);
                sayHelloFunc.Call();


                Func<int (*)(intintint) > sumFunc(sum);
                std::cout << "sumFunc.Call<int>(1, 2, 3) : " << sumFunc.Call<int>(1, 2, 3) << std::endl;


                std::cout << "\n---------------------------" << std::endl;

                Fn<> sayHelloFn(sayHello);
                sayHelloFn();
                
                Fn<intintintint> sumFn(sum);
                std::cout << "sumFn(1, 2, 3) : " << sumFn(1, 2, 3) << std::endl;

                std::cout << "\n---------------------------" << std::endl;

                return 0;
            }

            Hello world !
            sumFunc.Call<int>(1, 2, 3) : 6


            ---------------------------
            Hello world !
            sumFn(1, 2, 3) : 6


            ---------------------------

            上面的例子非常有趣,使用了2種方案,將一個函數,注冊到一個對象/仿函數中,并且通過一個對象/仿函數來直接調用調用。
            例子顯而易見的,第2種方案更佳簡潔,并且對傳遞參數有明確的判斷,當參數類型或數量不正確的時候,編譯器將導致失敗。
            這種方案,可以將類的成員變量直接作為函數的參數使用,或者,如我:
            http://blog.csdn.net/eclipser1987/article/details/23926395
            這篇文章中,無法直接調用腳本函數類,有了好的解決辦法。這個我將隨后補充。

            #include <list>
            #include <functional>

            template<typename Args>
            class Fns
            {
            private:

                std::list<std::function<void(Args)> > _calls;

            public:

                virtual ~Fns()
                {
                    _calls.clear();
                }

                void connect(std::function<void(Args)> fct)
                {
                    _calls.push_back(fct);
                }

                template<typename Object>
                void connect(Object* objectvoid (Object::*method)(Args))
                {
                    _calls.push_back([object,method](Args args){(*object.*method)(args);});
                }

                template<typename Object>
                void connect(Object* objectvoid (Object::*method)(Argsconst)
                {
                    _calls.push_back([object,method](Args args){(*object.*method)(args);});
                }

                template<typename Object>
                void connect(const Object* objectvoid (Object::*method)(Argsconst)
                {
                    _calls.push_back([object,method](Args args){(*object.*method)(args);});
                }

                void emit(Args args)
                {
                    for(auto call : _calls)
                        call(args);
                }
            };

            #include <cstdio>
            #include "Signal.hpp"

            class Foo
            {
            public:

                void bar(int x, int y)
                {
                    printf("Foo::bar(%d, %d)\n", x, y);
                }
            };

            void foobar(int x, int y)
            {
                printf("foobar(%d, %d)\n", x, y);
            }

            int main(void)
            {
                Foo foo;
                Fns<intint> s;

                // Connect a function
                s.connect(foobar);
                // Connect a class method
                s.connect(&foo, &Foo::bar);
                // Create and connect some lambda expression
                s.connect([&foo](int x, int y){ 
                    printf("lambda::"); foo.bar(x, y); 
                });
                // Emit the signal !
                s.emit(4, 2);
                getchar();
                return 0;
            }

            foobar(4, 2)
            Foo::bar(4, 2)
            lambda::Foo::bar(4, 2)
            posted on 2016-06-13 15:39 C++技術中心 閱讀(1426) 評論(0)  編輯 收藏 引用 所屬分類: C++ 基礎
            99久久精品久久久久久清纯| 欧美精品福利视频一区二区三区久久久精品 | 色婷婷综合久久久久中文一区二区| 久久久久久伊人高潮影院| 亚洲成av人片不卡无码久久| 国产99久久久国产精品小说| 国产精品免费福利久久| 久久国产午夜精品一区二区三区| 久久这里只有精品首页| 91精品国产综合久久久久久| 亚洲日韩欧美一区久久久久我| 精品久久久无码人妻中文字幕豆芽| 久久99国产亚洲高清观看首页 | 久久精品国产免费观看三人同眠| 99精品国产综合久久久久五月天 | A级毛片无码久久精品免费| 久久夜色精品国产亚洲| 久久久九九有精品国产| 久久人人爽人人爽人人片av麻烦| 久久青青草原综合伊人| 久久久久久亚洲精品成人| 色婷婷狠狠久久综合五月| 久久久中文字幕| 91久久婷婷国产综合精品青草 | 久久久久久国产a免费观看黄色大片 | 国产精品国色综合久久| 久久精品免费一区二区| 国产高清国内精品福利99久久| 国产成人精品综合久久久| 欧美日韩中文字幕久久久不卡| 伊人久久综合热线大杳蕉下载| 久久夜色精品国产噜噜麻豆| 欧美麻豆久久久久久中文| 中文字幕亚洲综合久久2| 精品蜜臀久久久久99网站| 人妻精品久久久久中文字幕69 | 99久久免费只有精品国产| 日本久久久精品中文字幕| 99久久国产综合精品成人影院| 久久免费精品视频| 爱做久久久久久|