指向类成员的指针

发布于 2024-08-23 09:10:49 字数 637 浏览 8 评论 0原文

我正在使用 Boost Spirit 解析器,当解析器进行解析时,语义操作会反映到 ParserActions 类的实例。

这是解析器的代码(相关部分)

struct urdf_grammar : public grammar<urdf_grammar> {

template <typename ScannerT>
        struct definition {

    definition(urdf_grammar const& self) {


        prog = (alpha_p >> *alnum_p)[&(self.actions.do_prog)];

    }

    rule<ScannerT> prog;

    rule<ScannerT> const&
            start() const {
        return prog;
    }
};

const ParserActions & actions;

explicit urdf_grammar(const ParserActions & actions = ParserActions()) : actions(actions) {
}  
};

I am using Boost Spirit parser, and as the parser is parsing, semantic actions are reflected to an instance of the class ParserActions.

Here is the code for the parser (the relevant part)

struct urdf_grammar : public grammar<urdf_grammar> {

template <typename ScannerT>
        struct definition {

    definition(urdf_grammar const& self) {


        prog = (alpha_p >> *alnum_p)[&(self.actions.do_prog)];

    }

    rule<ScannerT> prog;

    rule<ScannerT> const&
            start() const {
        return prog;
    }
};

const ParserActions & actions;

explicit urdf_grammar(const ParserActions & actions = ParserActions()) : actions(actions) {
}  
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

下壹個目標 2024-08-30 09:10:49

为了调用对象的成员函数,您需要提供两件事:

  1. 成员函数的地址,如之前所述,您可以通过编写 &my_class::my_member
  2. 指针(或引用)到您希望为 Spirit 语义操作调用成员函数的对象实例,

期望您提供公开特定接口的函数或函数对象。在您的情况下,预期的接口是:

void func(Iterator first, Iterator Last);

其中 Iterator 是用于调用解析函数的迭代器类型(可通过 typename ScannerT::iterator_t 访问)。您需要做的是创建一个公开上述接口的函数对象,同时仍然调用您的成员函数。虽然这可以手动完成,但这样做充其量是乏味的。创建函数对象的最简单方法是使用 Boost.Bind:

prog = (alpha_p >> *alnum_p)
       [
           boost::bind(&ParserActions::do_prog, self.action)
       ];

它将为您创建所需的函数对象,绑定并包装您的成员函数。

所有这些都假设您的成员函数不带任何参数。如果您需要传递一对迭代器,则将调用语义操作到您的函数,该构造需要编写为:

prog = (alpha_p >> *alnum_p)
       [
           boost::bind(&ParserActions::do_prog, self.action, _1, _2)
       ];

指示函数对象将其第一个和第二个参数转发给绑定函数。

In order to call a member function of an object you need to provide two things:

  1. the address of the member function, as said before you can get that by writing &my_class::my_member
  2. the pointer (or a reference) to the instance of the object you want the member function be invoked for

Spirit semantic actions expect you to provide a function or function object exposing a certain interface. In your case the expected interface is:

void func(Iterator first, Iterator Last);

where Iterator is the iterator type used to call the parse function (accessible through typename ScannerT::iterator_t). What you need to do is to create a function object exposing the mentioned interface while still calling your member function. While this can be done manually, doing so is tedious at best. The simplest way to create a function object is by using Boost.Bind:

prog = (alpha_p >> *alnum_p)
       [
           boost::bind(&ParserActions::do_prog, self.action)
       ];

which will create the required function object for you, binding and wrapping your member function.

All this assumes your member function does not take any arguments. If you need to pass the pair of iterators the semantic action will be invoked with to your function the construct needs to be written as:

prog = (alpha_p >> *alnum_p)
       [
           boost::bind(&ParserActions::do_prog, self.action, _1, _2)
       ];

instructing the function object to forward its first and second parameter to the bound function.

蓝海似她心 2024-08-30 09:10:49

要获取成员变量或函数的地址,请使用:

&MyClass::MyMember

要调用成员函数指针,请使用以下之一:

(my_class.*ptr_to_member)( /* arguments */ )
(my_ptr->*ptr_to_member)( /* arguments */ )

我在 Google 上找到的包含更多信息的随机链接:http://www.goingware.com/tips/member-pointers.html

To take the address of a member variable or function, use:

&MyClass::MyMember

To call a member function pointer, use one of:

(my_class.*ptr_to_member)( /* arguments */ )
(my_ptr->*ptr_to_member)( /* arguments */ )

Random link I found on Google with more info: http://www.goingware.com/tips/member-pointers.html

梦巷 2024-08-30 09:10:49

函数指针和成员函数指针之间存在根本区别:

  • 函数指针必须使用正确类型的参数调用,

  • a必须使用正确类型的对象和正确类型的参数来调用成员函数指针。

    必须

像从对象中那样构建成员函数指针不会在指针中注册该对象。

如果您想要这样的东西,有各种库可以这样做(您有一个 boost 标签,请参阅绑定和信号以获取此类示例)。

There is a fundamental difference between function pointers and member function pointers:

  • a function pointer must be called with arguments of the correct types,

  • a member function pointer must be called with an object of the correct type and arguments of the correct types.

Building a member function pointer like you do from an object does not register the object in the pointer.

If you want such a thing, there are various libraries doing so (you have a boost tag, see bind and signals for examples of such).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文