C++应用于对象的成员函数

发布于 2024-08-19 18:52:52 字数 377 浏览 4 评论 0原文

我想通过将成员函数作为模板参数传递来调用它,而不使用 boost 是可能的。这是我尝试做的一个示例,

class object { void method(); }

{
object object_instance;
...
apply<object:: method>();
...

template<class F>
void apply() { F(object_instance); } // want to call object_instance.F()
}

但它不起作用,所以问题是如何将对象方法绑定到对象。 谢谢

上面是一个例子,不是真正的代码。我有一堆仅名称不同但具有许多参数的函数,我想将它们包装在运算符中。

I want to call member function by passing it as template parameter, without using boost is possible. Here is an example off what I tried to do,

class object { void method(); }

{
object object_instance;
...
apply<object:: method>();
...

template<class F>
void apply() { F(object_instance); } // want to call object_instance.F()
}

that does not work, so question is how do I go about binding object method to an object.
Thanks

above is an example, not the real code. I have a bunch of functions differing only in name, but with many parameters, that I want to wrap around in operators.

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

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

发布评论

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

评论(4

↙厌世 2024-08-26 18:52:52

比如:

struct foo
{
    void bar(void) {}
};

template <typename R, typename C>
R apply(C& pObject, R (C::*pFunc)())
{
    return (pObject.*pFunc)();
}

int main(void)
{
    foo f;
    apply(f, &foo::bar);
}

这个。

Something like:

struct foo
{
    void bar(void) {}
};

template <typename R, typename C>
R apply(C& pObject, R (C::*pFunc)())
{
    return (pObject.*pFunc)();
}

int main(void)
{
    foo f;
    apply(f, &foo::bar);
}

this.

独夜无伴 2024-08-26 18:52:52

这与您的代码类似,并且允许将成员函数作为模板参数传递:

class object { public: void method() {} };
object object_instance;

template<void (object::*F)()>
void apply() {
    (object_instance.*F)();
}

int main() {
    apply<&object::method>();
    return 0;
}

This is similar to your code, and will allow passing a member function as a template parameter:

class object { public: void method() {} };
object object_instance;

template<void (object::*F)()>
void apply() {
    (object_instance.*F)();
}

int main() {
    apply<&object::method>();
    return 0;
}
失退 2024-08-26 18:52:52

由于成员函数不是类型或整数值,因此不能将其作为模板参数传递。您可以通过创建一个调用成员函数的结构来完成您想要的操作,尽管在模板的范围内使用 object_instance 相当难闻。

你到底想做什么?

As a member function isn't a type or integral value, you can't pass it as a template parameter. You could do what you seem to want by creating a struct which calls the member function, though having object_instance in scope in the template is rather smelly.

What are you actually trying to do?

此示例适用于 c++ox 中的右值引用。不适用于所有编译器。

class Sample
{
public:
    void fun() { cout << "Sample::fun\n"; }
};

template<typename Function>
void FunCall( Function&& f)
{
    f();
}

void RvaluesDemo()
{
    FunCall([&]
    {
        Sample().fun();
    });
}

This example is for rvalue references in c++ox. Does not work on all compilers.

class Sample
{
public:
    void fun() { cout << "Sample::fun\n"; }
};

template<typename Function>
void FunCall( Function&& f)
{
    f();
}

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