<bdo id='xH54o'></bdo><ul id='xH54o'></ul>
  1. <small id='xH54o'></small><noframes id='xH54o'>

  2. <legend id='xH54o'><style id='xH54o'><dir id='xH54o'><q id='xH54o'></q></dir></style></legend>

      <i id='xH54o'><tr id='xH54o'><dt id='xH54o'><q id='xH54o'><span id='xH54o'><b id='xH54o'><form id='xH54o'><ins id='xH54o'></ins><ul id='xH54o'></ul><sub id='xH54o'></sub></form><legend id='xH54o'></legend><bdo id='xH54o'><pre id='xH54o'><center id='xH54o'></center></pre></bdo></b><th id='xH54o'></th></span></q></dt></tr></i><div id='xH54o'><tfoot id='xH54o'></tfoot><dl id='xH54o'><fieldset id='xH54o'></fieldset></dl></div>
      <tfoot id='xH54o'></tfoot>

    1. std::成员函数指针的映射?

      时间:2024-05-11

        <tbody id='8yimU'></tbody>
      • <bdo id='8yimU'></bdo><ul id='8yimU'></ul>
        <tfoot id='8yimU'></tfoot>
        <i id='8yimU'><tr id='8yimU'><dt id='8yimU'><q id='8yimU'><span id='8yimU'><b id='8yimU'><form id='8yimU'><ins id='8yimU'></ins><ul id='8yimU'></ul><sub id='8yimU'></sub></form><legend id='8yimU'></legend><bdo id='8yimU'><pre id='8yimU'><center id='8yimU'></center></pre></bdo></b><th id='8yimU'></th></span></q></dt></tr></i><div id='8yimU'><tfoot id='8yimU'></tfoot><dl id='8yimU'><fieldset id='8yimU'></fieldset></dl></div>

          <small id='8yimU'></small><noframes id='8yimU'>

                <legend id='8yimU'><style id='8yimU'><dir id='8yimU'><q id='8yimU'></q></dir></style></legend>
                本文介绍了std::成员函数指针的映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我需要用 对实现一个 std::map.函数指针是指向拥有映射的同一类的方法的指针.这个想法是直接访问方法,而不是实现 switch 或等效的.

                I need to implement an std::map with <std::string, fn_ptr> pairs. The function pointers are pointers to methods of the same class that owns the map. The idea is to have direct access to the methods instead of implementing a switch or an equivalent.

                (我使用 std::string 作为地图的键)

                ( I am using std::string as keys for the map )

                我对 C++ 很陌生,所以有人可以发布一些伪代码或链接来讨论使用函数指针实现映射吗?(指向拥有地图的同一类所拥有的方法的指针)

                I'm quite new to C++, so could anyone post some pseudo-code or link that talks about implementing a map with function pointers? ( pointers to methods owned by the same class that owns the map )

                如果您认为有更好的方法可以解决我的问题,也欢迎提出建议.

                If you think there's a better approach to my problem, suggestions are also welcome.

                推荐答案

                这是我能想到的最简单的方法.请注意没有错误检查,地图可能会被设为静态.

                This is about the simplest I can come up with. Note no error checking, and the map could probably usefully be made static.

                #include <map>
                #include <iostream>
                #include <string>
                using namespace std;
                
                struct A {
                    typedef int (A::*MFP)(int);
                    std::map <string, MFP> fmap;
                
                    int f( int x ) { return x + 1; }
                    int g( int x ) { return x + 2; }
                
                
                    A() {
                        fmap.insert( std::make_pair( "f", &A::f ));
                        fmap.insert( std::make_pair( "g", &A::g ));
                    }
                
                    int Call( const string & s, int x ) {
                        MFP fp = fmap[s];
                        return (this->*fp)(x);
                    }
                };
                
                int main() {
                    A a;
                    cout << a.Call( "f", 0 ) << endl;
                    cout << a.Call( "g", 0 ) << endl;
                }
                

                这篇关于std::成员函数指针的映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:std::sort() 中使用哪种类型的排序? 下一篇:关于使用用户定义方法扩展 C++ STL 容器的更好方法的建议

                相关文章

                <i id='EKI3z'><tr id='EKI3z'><dt id='EKI3z'><q id='EKI3z'><span id='EKI3z'><b id='EKI3z'><form id='EKI3z'><ins id='EKI3z'></ins><ul id='EKI3z'></ul><sub id='EKI3z'></sub></form><legend id='EKI3z'></legend><bdo id='EKI3z'><pre id='EKI3z'><center id='EKI3z'></center></pre></bdo></b><th id='EKI3z'></th></span></q></dt></tr></i><div id='EKI3z'><tfoot id='EKI3z'></tfoot><dl id='EKI3z'><fieldset id='EKI3z'></fieldset></dl></div>

              1. <legend id='EKI3z'><style id='EKI3z'><dir id='EKI3z'><q id='EKI3z'></q></dir></style></legend>

                1. <small id='EKI3z'></small><noframes id='EKI3z'>

                  • <bdo id='EKI3z'></bdo><ul id='EKI3z'></ul>

                  <tfoot id='EKI3z'></tfoot>