。有没有办法来调用成员函数没有*或 - > *运算符 [英] Is there a way to call member function without .* or ->* operator

查看:1067
本文介绍了。有没有办法来调用成员函数没有*或 - > *运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下调用 D :: foo的函数通过指针到成员函数会产生错误的方法:必须使用 * - > * 来调用指针到成员函数F(...)
当然..这不是我们如何调用指针到成员函数。

Below method of calling D::foo function via pointer-to-member function will generate error: must use .* or ->* to call pointer-to-member function in 'f (...)' .. of course that is not how we call pointer-to-member functions.

调用的正确方法是(D * F。)(5); (对GT; * F)(5) ;

我的问题是,有没有办法来调用类的成员函数,而不在左边的类对象?我不知道,我们可以通过类对象(这个)作为常规参数?

My question is, 'Is there a way to call member function of a class without the class object on left hand side? I wonder if we could pass class object (this) as regular argument?

在我的脑海里,在天(装配/二进制级别)的年底类的所有成员函数都是应该在N + 1个参数,其中操作(+1是这正常功能

In my mind, at end of the day (at assembly/binary level) all member functions of a class are normal functions which should operate on n + 1 arguments where (+1 is for this)

如果我们谈论 D :: foo的下面的功能,在组装/二进制级别应该对两个参数进行操作:

If we talk about D::foo function below, at assembly/binary level it should operate on two arguments:


  1. 类对象本身(指针称为D类对象这个

  2. INT

  1. The class object itself (pointer to class D object called this)
  2. and the int.

那么,有没有办法(或黑客)来调用 D :: foo的与传递给它的函数参数,而不是使用类对象 。或者 - > 。或* - > * 类对象运营商

so, is there a way (or hack) to call D::foo with class object passed to it as function argument instead of using . or -> or .* or ->* operators on class object?

样品code:

#include <iostream>
using namespace std;

class D {
    public:
        void foo ( int a ) {
            cout << "D" << endl;
        }

        int data;
};


//typedef void  __cdecl ( D::*  Func)(int);
typedef void ( D::*  Func)(int);

int main ( void ) 
{
    D d;

    Func f = &D::foo;
    f(&d, 5);

    return 1;
 }

一种方法是使用升压绑定即

One method is using boost bind i.e

(boost:: bind (&D::foo, &d, 5)) ();

编辑:
请注意,我不是在寻找一个版本的程序,它的工作原理,我知道如何使它工作的

"Please note I am not looking for a version of this program which works, I know how to make it work"

推荐答案

您真的想调用一个成员函数,而无需使用 - &GT ; ?真的,真正?好吧,好吧......

You really want to call a member function without using . or ->? Really, really? Well, okay...

Evil.h:

#ifdef __cplusplus
extern "C" {
#endif

struct MyStruct
{
#ifdef __cplusplus
    MyStruct();

    void method(int);
#endif
};

#ifdef __cplusplus
}
#endif

Evil.cc:

Evil.cc:

#include <iostream>

#include "evil.h"

MyStruct::MyStruct() { std::cout << "This is MyStruct's constructor" << std::endl; }

void MyStruct::method(int i) { std::cout << "You passed " << i << std::endl; }

Evil.c:

Evil.c:

#include "evil.h"

int main()
{
    struct MyStruct my_struct;
    _ZN8MyStructC1Ev(&my_struct); /* MyStruct::MyStruct() */

    _ZN8MyStruct6methodEi(&my_struct, 3); /* MyStruct::method(int) */

    return 0;
}

这正好为我的gcc和g ++在Linux上的结合工作,但不用说它依赖于平台ABI,并违反了C89标准调用格式的功能的强调,大写字母的。它几乎可以肯定不会有虚函数的工作,我不倾向于尝试。这也可能是我写过的最邪恶的东西。但仍...

This happens to work for my combination of gcc and g++ on Linux, but needless to say it relies on the platform ABI, and violates the C89 standard in calling functions of the form underscore-capital letter. It almost certainly won't work with virtual functions, and I'm not inclined to try. It may also be the most evil thing I've ever written. But still...

编辑:引述OP:

在我的脑海里,在天(装配/二进制级别)的年底类的所有成员函数都是应该在N + 1个参数,其中操作(+1是这正常功能

In my mind, at end of the day (at assembly/binary level) all member functions of a class are normal functions which should operate on n + 1 arguments where (+1 is for this)

虽然这是真的,既然CFRONT每一个编译器已经这样做了,这只是一个实现细节。 C ++标准是煞费苦心的以指定成员函数应该如何实现的,他们应该如何行事。

While it's true that every compiler since CFront has done it this way, that's just an implementation detail. The C++ standard is at pains not to specify how member functions should implemented, just how they should behave.

由于它是一个实现细节,不同的平台做不同的方式。这超出了只是名字改编。例如,在Linux中使用的调用约定指定这个作为第一个参数传递;其他实现(Borland公司,IIRC?)通这个作为的最后的参数。

Because it's an implementation detail, different platforms do it in different ways. This goes beyond just name mangling. For example, the calling convention used on Linux specifies that this is passed as the first argument; other implementations (Borland, IIRC?) pass this as the last argument.

所以,如果你想治疗成员函数普通函数有一个额外的这个,那么你必须自己限制在一个特定的ABI。这篇文章提供的你可能会怎么做一个例子(或者说,为什么你真的不应该那样做一个例子!)

So, if you want to treat member functions as ordinary functions with an extra this, then you have to restrict yourself to a particular ABI. This post serves an example of how you might do that (or rather, an example of why you really shouldn't do that!)

那么,有没有办法(或破解),以称D :: foo且传递给它的函数参数,而不是使用类对象。或 - >或* - > *类对象运营商

so, is there a way (or hack) to call D::foo with class object passed to it as function argument instead of using . or -> or .* or ->* operators on class object?

一个特定平台,恶心肮脏的黑客...

A platform-specific, disgusting dirty hack...

这篇关于。有没有办法来调用成员函数没有*或 - &GT; *运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆