指向成员函数的指针问题

发布于 2024-09-29 03:59:19 字数 443 浏览 0 评论 0原文

在下面的代码中(请参阅评论):

#include "stdafx.h"

#include <iostream>
using std::cout;

struct Base
{
 void fnc()
 {
  cout << "Base::fnc()";
 }

};

struct Impl
{
 void* data_;
 Impl(void (Base::*fp)())
 {
  fp();//HERE I'M INVOKING IT - I'M DOING SOMETHING WRONG!
 }
};
int _tmain(int argc, _TCHAR* argv[])
{
 return 0;
}  

错误
“错误 1 ​​错误 C2064:术语不计算为采用 0 个参数的函数” 为什么它不起作用以及如何修复它?

In code below (please see comment):

#include "stdafx.h"

#include <iostream>
using std::cout;

struct Base
{
 void fnc()
 {
  cout << "Base::fnc()";
 }

};

struct Impl
{
 void* data_;
 Impl(void (Base::*fp)())
 {
  fp();//HERE I'M INVOKING IT - I'M DOING SOMETHING WRONG!
 }
};
int _tmain(int argc, _TCHAR* argv[])
{
 return 0;
}  

Error
"Error 1 error C2064: term does not evaluate to a function taking 0 arguments"
Why doesn't it work and how to fix it?

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

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

发布评论

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

评论(5

汹涌人海 2024-10-06 03:59:19

它不起作用,因为 fp 不是函数指针而是成员指针。

如何修复它很简单,按照应该使用的方式使用它:someinstance.*fp();

It doesn't work because fp is not a function pointer but a member pointer.

How to fix it is easy, use it as it should be used: someinstance.*fp();

断爱 2024-10-06 03:59:19

问题是您将该函数作为自由函数调用,而实际上它不是。它是一个成员函数,您需要在对象的上下文中调用它:

(obj.*f)();

Boost.Bind 提供了一种惯用的方法来解决这个问题:

#include<boost/bind.hpp>

// ...
Impl i(boost::bind(&Base::fnc, obj));

您可以像这样定义 Impl 构造函数:

#include<boost/function.hpp>

// ...
Impl(boost::function<void ()> fnc)
{
    fnc();  // boost::bind translates it to obj.fnc()
}

如果只有 Impl 对象知道要调用该函数的对象,那么您可以使用 Boost.Bind 的占位符:

Impl i(boost::bind(&Base::fnc, boost::_1));

然后 Impl 构造函数将类似于

Impl(boost::function<void (Base)> fnc, Base& b)
{
    fnc(b);  // boost::bind translates it to b.fnc()
}

有时它是明智的使用模板接受函子的一侧:

template<class Op>
Impl(Op fnc) { ... }

因为这样客户端就可以传递任何成员函数,无论有没有 boost。但代价是您可能会遇到更难以理解的编译器错误消息。

The problem is that you are calling the function as a free function, when it isn't. It's a member function, and you need to call it in the context of an object:

(obj.*f)();

Boost.Bind offers an idiomatic way to tackle this:

#include<boost/bind.hpp>

// ...
Impl i(boost::bind(&Base::fnc, obj));

You can define the Impl constructor like so:

#include<boost/function.hpp>

// ...
Impl(boost::function<void ()> fnc)
{
    fnc();  // boost::bind translates it to obj.fnc()
}

If only the Impl object knows what object to call the function on, then you can use the placeholders of Boost.Bind:

Impl i(boost::bind(&Base::fnc, boost::_1));

And the Impl constructor would then be similar to

Impl(boost::function<void (Base)> fnc, Base& b)
{
    fnc(b);  // boost::bind translates it to b.fnc()
}

Sometimes it's wiser to use templates in the side that accepts the functor:

template<class Op>
Impl(Op fnc) { ... }

because then the client can pass any member function, with or without boost. But the cost is that you might have harder-to-understand compiler error messages.

极致的悲 2024-10-06 03:59:19
typedef  int (MyClass::*memberPointer_t)(int);

...

memberPointer_t mb = &MyClass::function;
MyClass* object = getObject();

int returnValue = (object->*mb)(3);

...

由于它是指向成员函数的指针,因此必须在对象上调用它并使用 ->* 或 .* 运算符来调用它。

typedef  int (MyClass::*memberPointer_t)(int);

...

memberPointer_t mb = &MyClass::function;
MyClass* object = getObject();

int returnValue = (object->*mb)(3);

...

Since it's a pointer to a member function, you must call it on an object and use the ->* or the .* operator to call it.

明月松间行 2024-10-06 03:59:19

您需要一个基础来调用该函数。

您可能正在寻找更像是 bind() 和 function<> 的东西。这将允许您将实例和成员函数绑定到可以像函数一样调用的函子中。

You need a Base to call the function on.

You might be looking for something more like bind() and function<> that will allow you to bind an instance and member function into a functor that can be just called like a function.

憧憬巴黎街头的黎明 2024-10-06 03:59:19

您可以阅读有关指向成员的常见问题解答功能。
他们特别强烈建议您为这些调用定义一个宏:

#define CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember))

You could benefit from reading this FAQ regarding pointers to member functions.
Particularly they strongly recommend you define a macro for these calls:

#define CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember))

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