<small id='TWxvl'></small><noframes id='TWxvl'>

      1. <i id='TWxvl'><tr id='TWxvl'><dt id='TWxvl'><q id='TWxvl'><span id='TWxvl'><b id='TWxvl'><form id='TWxvl'><ins id='TWxvl'></ins><ul id='TWxvl'></ul><sub id='TWxvl'></sub></form><legend id='TWxvl'></legend><bdo id='TWxvl'><pre id='TWxvl'><center id='TWxvl'></center></pre></bdo></b><th id='TWxvl'></th></span></q></dt></tr></i><div id='TWxvl'><tfoot id='TWxvl'></tfoot><dl id='TWxvl'><fieldset id='TWxvl'></fieldset></dl></div>
        • <bdo id='TWxvl'></bdo><ul id='TWxvl'></ul>
      2. <legend id='TWxvl'><style id='TWxvl'><dir id='TWxvl'><q id='TWxvl'></q></dir></style></legend>
        <tfoot id='TWxvl'></tfoot>
      3. 如何调用指向成员函数的指针?

        How do I call a pointer-to-member-function?(如何调用指向成员函数的指针?)

      4. <tfoot id='GC4Vy'></tfoot>

        1. <legend id='GC4Vy'><style id='GC4Vy'><dir id='GC4Vy'><q id='GC4Vy'></q></dir></style></legend>
            <bdo id='GC4Vy'></bdo><ul id='GC4Vy'></ul>

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

                  <tbody id='GC4Vy'></tbody>

                  本文介绍了如何调用指向成员函数的指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我收到了一个我不明白的编译错误 (MS VS 2008).搞了好几个小时之后,一切都变得模糊了,我觉得我错过了一些非常明显(而且非常愚蠢)的东西.这是基本代码:

                  I'm getting a compile error (MS VS 2008) that I just don't understand. After messing with it for many hours, it's all blurry and I feel like there's something very obvious (and very stupid) that I'm missing. Here's the essential code:

                  typedef int (C::*PFN)(int);
                  
                  struct MAP_ENTRY
                      {
                      int id;
                      PFN pfn;
                      };
                  
                  class C
                      {
                      ...
                      int Dispatch(int, int);
                      MAP_ENTRY *pMap;
                      ...
                      };
                  
                  int C::Dispatch(int id, int val)
                      {
                      for (MAP_ENTRY *p = pMap; p->id != 0; ++p)
                          {
                          if (p->id == id)
                              return p->pfn(val);  // <--- error here
                          }
                      return 0;
                      }
                  

                  编译器在箭头处声称术语不会评估为采用 1 个参数的函数".为什么不?PFN 的原型是一个带一个参数的函数,而 MAP_ENTRY.pfn 是一个 PFN.我在这里错过了什么?

                  The compiler claims at the arrow that the "term does not evaluate to a function taking 1 argument". Why not? PFN is prototyped as a function taking one argument, and MAP_ENTRY.pfn is a PFN. What am I missing here?

                  推荐答案

                  p->pfn 是指向成员函数类型的指针.为了通过这样的指针调用函数,您需要使用运算符 ->* 或运算符 .* 并提供 C 作为左操作数.你没有.

                  p->pfn is a pointer of pointer-to-member-function type. In order to call a function through such a pointer you need to use either operator ->* or operator .* and supply an object of type C as the left operand. You didn't.

                  我不知道应该在这里使用哪种 C 类型的对象 - 只有你知道 - 但在你的例子中它可能是 *这个.在这种情况下,调用可能如下所示

                  I don't know which object of type C is supposed to be used here - only you know that - but in your example it could be *this. In that case the call might look as follows

                  (this->*p->pfn)(val)
                  

                  为了让它看起来不那么复杂,可以引入一个中间变量

                  In order to make it look a bit less convoluted, you can introduce an intermediate variable

                  PFN pfn = p->pfn;
                  (this->*pfn)(val);
                  

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Is Type(::x); valid?(是类型(::x);有效的?)
                  Difference between an inline function and static inline function(内联函数和静态内联函数的区别)
                  Compilation fails randomly: quot;cannot open program databasequot;(编译随机失败:“无法打开程序数据库)
                  Too many initializers error for a simple array in bcc32(bcc32 中的简单数组的初始值设定项过多错误)
                  No Member named stoi in namespace std(命名空间 std 中没有名为 stoi 的成员)
                  Error using a constexpr as a template parameter within the same class(在同一个类中使用 constexpr 作为模板参数时出错)
                  <legend id='m3Jvt'><style id='m3Jvt'><dir id='m3Jvt'><q id='m3Jvt'></q></dir></style></legend>
                    <bdo id='m3Jvt'></bdo><ul id='m3Jvt'></ul>
                  • <small id='m3Jvt'></small><noframes id='m3Jvt'>

                        <tfoot id='m3Jvt'></tfoot>

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