【问题标题】:What is the syntax for calling a member function pointer that is a member of a structure array in C++调用作为C++中结构数组成员的成员函数指针的语法是什么
【发布时间】:2012-03-12 21:31:17
【问题描述】:

这个问题类似于我正在尝试做的 Calling C++ member function pointer from a struct 。 但是,我的结构包含一个成员函数指针,该指针在不同的类中定义,然后是在其中定义和使用该结构的类。这是一些示例代码,说明了我的类、结构和函数指针的布局方式。

// Alpha.h:
class Alpha{
public:
    void function1(char name[], int number);
    void function2(char name[], int number);
    void function3(char name[], int number);

    typedef void (Alpha::*My_func_ptr)(char name[], int number);

    static My_func_ptr functionTable[];
};

// Alpha.cpp:
#include "Alpha.h"

Alpha::My_func_ptr Alpha::functionTable[] = {
    &Alpha::function1, 
    &Alpha::function2, 
    &Alpha::function3
};

void Alpha::function1(char name[], int number)
{
    //some stuff
}

void Alpha::function2(char name[], int number)
{
    //some stuff
}

void Alpha::function3(char name[], int number)
{
    //some stuff
}

// Beta.h:
#include "Alpha.h"

typdef struct{
    char bName[10];
    Alpha::My_func_ptr fptr;
}ptr_structure;

class Beta{
public:
      void betafunction();

      Alpha alphaobject;
      ptr_structure str_array[3];
};

// Beta.cpp:
#include "Beta.h"

void betafunction()
{
    str_array[0].fptr = alphaobject.functionTable[0];
    str_array[1].fptr = alphaobject.functionTable[1];
    str_array[2].fptr = alphaobject.functionTable[2];

    (str_array[0].fptr)("name", 1); //gives error expression must have 
                                    //(pointer-to-)  function type

    (this->*str_array[0].fptr)("name", 1);
    //error pointer-to-member selection class types are incompatible "Beta" and "Alpha"

    //sample function pointer call using function table from other class,
    //this syntax compiles and runs without error.
    (alphaobject.*Alpha::functionTable[0]("name", 1); 
}

如您所见,我可以从数组中调用函数指针,但似乎无法弄清楚如何从结构数组中调用函数指针。

【问题讨论】:

  • 请尝试区分function pointermember function pointer,它们非常不同。
  • 另外,您的代码无法编译(例如,一个错字typdef)。始终照原样复制代码。
  • 谢谢 Jesse,我应该编辑标题还是将其添加到问题文本中?
  • Ed S,感谢您的建议,但是这将涉及几个大文件和大量无关信息,这些信息会减损与我的问题相关的代码的重要部分。
  • 不,我只是想复制和粘贴,而不是输入内容。我假设您的代码确实可以编译,但不会因为拼写错误。

标签: c++ arrays function-pointers structure


【解决方案1】:

当通过成员函数指针调用时,你需要有一个与该指针关联的对象的实例:

 (alphaobject.*(str_array[0].fptr))("name", 1)
  ^^^^^^^^^^^

【讨论】:

  • 非常感谢迈克尔,这就是答案。我猜是因为我正在调用在 Alpha 内部声明的函数指针,我需要该类的一个对象来访问该指针。你有什么建议阅读可以帮助我更好地理解你的答案吗?
【解决方案2】:

我认为:

(object.*functionTable[0])(args, ...);
(objptr->*functionTable[0])(args, ....);

IIRC,object.* 运算符的组合就像一个大的一元运算符。所以它的优先级低于[0] 后缀。但是,它的优先级也低于函数调用后缀运算符(args, ...)

类比:

(*foo)();  /* classic C */

当然,在调用常规函数时不需要* 运算符。但如果你写了它,你需要括号,因为*foo() 意味着别的东西。

【讨论】:

  • 感谢您的输入,但 object.*Alpha::functionTable[0] 调用是工作语法的一个示例,我认为这可能对人们有所帮助。我正在尝试让函数指针调用 (str_array[0].fptr)(args) 工作。
  • 答案来自这个。你刚刚拿走了functionTable[0] 并将其存储在其他地方。因此,将表达式 functionTable[0] 替换为访问“其他地方”的表达式。你仍然需要这个对象;如果没有对象所属的类,则不能调用指向成员的指针。 IE。 (object.*(EXPR))(args, ...);其中 EXPR 引用指向成员的指针,无论它在哪里。当然,指向成员的类必须与对象的类匹配!
  • 另外,你为什么在非成员函数中使用this?你期望它评估什么?那个东西的类型是什么?
  • 你是对的,当你说用访问它为我点击的相同内存的东西替换 functionTable[0] 时。 “这个”部分来自对类似问题的回答,我想表明该答案不起作用。再次感谢您的帮助。
  • 有时对代码进行代数操作会有所帮助。这个表达式有效,好吧,那么如果我们用另一个意思相同的子表达式替换这个子表达式呢?应该都是一样的。
【解决方案3】:

您可以选择两种解决方案之一,具体取决于您希望代码的可读性如何。不可读的版本(甚至可能是错误的,我什至不会尝试编译):

void Beta::betafunction() {
   Alpha a;
   (a.*(strArray[0].fptr))("name",1);
}

但我实际上会尝试让事情变得更简单:

void Beta::betafunction() {
   Alpha a;
   Alpha::My_func_ptr mptr = strArray[0].fptr;
   (a.*mptr)("name",1);
}

我相信第二个更易读,编译器可以很容易地优化掉mptr,所以尝试使用语法来玩guru是没有意义的。

【讨论】:

    猜你喜欢
    • 2010-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-27
    相关资源
    最近更新 更多