简体   繁体   English

如何用非类函数指针调用类成员函数?

[英]How to call class member functions with non-class function pointer?

So it seems ANTLR3C uses function pointers to allow users to override them if we want to write our own functions. 因此,如果我们想编写自己的函数,似乎ANTLR3C使用函数指针允许用户覆盖它们。 I need to override the error reporting function: 我需要覆盖错误报告功能:

Pointer 指针

void(*displayRecognitionError)(struct ANTLR3_BASE_RECOGNIZER_struct * recognizer,
                                 pANTLR3_UINT8 * tokenNames);

Function 功能

static void displayRecognitionError (pANTLR3_BASE_RECOGNIZER recognizer,
                                     pANTLR3_UINT8 * tokenNames);

Right now I have the following: 现在我有以下内容:

@lexer::apifuncs {
  RECOGNIZER->displayRecognitionError = displayRecognitionErrorNew;
}

@lexer::includes {
  #include "lexerData.h"

  static void displayRecognitionErrorNew (pANTLR3_BASE_RECOGNIZER recognizer,
                                          pANTLR3_UINT8 * tokenNames);
}

@lexer::members {
  #include "lexerError.h"
}

with the new error code(which all works) in the file lexerError.h. 使用lexerError.h文件中的新错误代码(全部有效)。 I'm trying to change this by moving the error function inside the lexerData class but unfortunately I get a compile error when try to set the function pointer to the class member. 我试图通过移动lexerData类中的错误函数来改变它,但不幸的是,当我尝试将函数指针设置为类成员时,我得到了编译错误。

compileUnit.cpp:179:62: error: argument of type ‘void (lexerData::)(ANTLR3_BASE_RECOGNIZER_struct*, uint8_t**)’ does not match ‘void (*)(ANTLR3_BASE_RECOGNIZER_struct*, uint8_t**)’

From what I've read this isn't a simple casting or syntax problem. 从我所读到的,这不是一个简单的转换或语法问题。 What should I do to allow this? 我应该怎么做才能允许这个? One fix appears to be change the function pointer definition but I need to able to assign the function pointer to different classes as the lexer is run twice with different data structures. 一个修复似乎是更改函数指针定义,但我需要能够将函数指针分配给不同的类,因为词法分析器使用不同的数据结构运行两次。

Generally, it is not possible to simply assign address of member non-static function to pointer to non-class function. 通常,不可能简单地将成员非静态函数的地址分配给指向非类函数的指针。 You can create non-class function which will call the proper member function of the lexerData class. 您可以创建非类函数,该函数将调用lexerData类的正确成员函数。 The problem is that this non-class function must be able to reference the lexerData object in some way. 问题是这个非类函数必须能够以某种方式引用lexerData对象。 I don't know anything about ANTLR3C so, unfortunately, I can't give you a specific advice about that :( 我对ANTLR3C一无所知所以,不幸的是,我不能给你一个具体的建议:(

The answer lies within this question . 答案就在于这个问题

Also boost::bind may help you. boost::bind也可以帮到你。 I do not have much information about it though. 虽然我没有太多关于它的信息。

In C++ each member function of the class has a hidden parameter. 在C ++中,类的每个成员函数都有一个隐藏参数。 It is a pointer to the instance of the class ( this ). 它是指向类实例的指针( this )。 The implementation of this feature is not standardized, so you cannot assume that this parameter can be somehow "found" with some specific signature like int foo(const MyClass*, int, int); 此功能的实现不是标准化的,因此您不能假设此参数可以某种方式“找到”某些特定的签名,如int foo(const MyClass*, int, int); instead of int foo(int, int); 而不是int foo(int, int); for the class MyClass . 对于MyClass类。

Generally this means that if you have a member function of some class and want to have a pointer to it, you have a problem. 通常这意味着如果你有一个类的成员函数,并希望有一个指针,你有一个问题。 I remember that in one project we used C macros + the table of methods and classes was used to convert "pointers" to functions to real calls to functions. 我记得在一个项目中我们使用了C宏+方法和类的表用于将“指针”转换为函数以实现对函数的实际调用。 The general idea was to "register" the instance of the class with macro in some global table and later to call the method of the class using the additional dispatch via this table. 一般的想法是在一些全局表中用宏“注册”类的实例,然后通过该表使用附加的调度来调用类的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM