• <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++技術中心 閱讀(1413) 評論(0)  編輯 收藏 引用 所屬分類: C++ 基礎
            国产精品国色综合久久| 亚洲国产精品综合久久网络| 性欧美丰满熟妇XXXX性久久久| 国内精品久久久久久久久电影网| 亚洲精品午夜国产VA久久成人| 人妻久久久一区二区三区| 久久久久亚洲AV无码麻豆| 精品久久久久久久| 秋霞久久国产精品电影院| 久久黄色视频| 国内精品久久久久影院一蜜桃 | 久久精品无码一区二区无码| 好久久免费视频高清| 韩国三级大全久久网站| 一本久道久久综合狠狠躁AV| 97久久综合精品久久久综合| 亚洲综合久久久| 久久国产免费观看精品3| 欧美国产精品久久高清| 国产精品无码久久综合| 日日狠狠久久偷偷色综合免费 | 久久成人小视频| 久久精品国产91久久综合麻豆自制| 久久亚洲精品无码播放| 久久99国产精品久久99果冻传媒| 久久久久亚洲av综合波多野结衣| 四虎国产永久免费久久| 久久人人妻人人爽人人爽| 无码任你躁久久久久久老妇| 久久免费高清视频| 久久亚洲精品成人AV| 久久久精品国产免大香伊| 久久久久国产一区二区| 久久亚洲国产欧洲精品一| 久久久精品2019免费观看| 久久精品国产日本波多野结衣| 久久综合九色综合久99| 香蕉99久久国产综合精品宅男自 | 久久99国产精一区二区三区| 国产精品久久国产精麻豆99网站 | 久久99精品九九九久久婷婷|