简体   繁体   English

将“this”参数显式传递给方法调用

[英]Explicit passing “this” parameter to method call

Is it possible in c++ call class method with explicit passing first "this" param to it? 是否有可能在c ++调用类方法中显式传递第一个“this”参数呢?

Something like this: 像这样的东西:

struct A
{
    void some() {} 
};

....

A a;
A::some(&a); // ~ a.some();

For reasonable question "WHY?": i need to implement std::bind analogue, and it works fine with constructions like this: 对于合理的问题“为什么?”:我需要实现std :: bind analogue,它适用于这样的结构:

void f(int);
bind(f, 3);

but this doesn't work: 但这不起作用:

bind(&A::some, &a);

UPDATE: Guys, my question is obviously not really clear. 更新:大家好,我的问题显然不是很清楚。 I know how to use std::bind, i want to know HOW is it processing constructions where this param explicitly passed to it: std::bind(&A::some, &a); 我知道如何使用std :: bind,我想知道如何处理这个param显式传递给它的构造:std :: bind(&A :: some,&a);

Here is an idea for a dispatcher which you could use inside your bind : 以下是您可以在bind使用的调度程序的想法:

template <class R, class... Arg>
R call(R (*f)(Arg...), Arg &&... arg)
{ return f(std::forward<Arg>(arg)...); }

template <class C, class R, class... Arg>
R call(R (C::*f)(Arg...), C &c, Arg &&... arg)
{ return (c.*f)(std::forward<Arg>(arg)...); }

Do you want something like the following ? 你想要下面这样的东西吗?

struct A
{
    void some();
    static void some(A* that) { that->some(); } 
};

..

A a;
A::some(&a);

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

相关问题 如何将 class 方法作为参数传递给另一个 function 并稍后调用它,最好使变量 class 方法签名显式? - How can I pass a class method as a parameter to another function and later call it, preferably making the variable class method signature explicit? 有没有办法通过引用传递,并在函数调用中显式传递值? - Is there a way to make passing by reference, and passing by value explicit in the function call? 将方法作为构造函数参数传递 - Passing a method as constructor parameter 用空参数包显式调用variadic函数模板 - explicit call to variadic function template with empty parameter pack 显式调用模板参数类型的析构函数,即使在内置实例化时也是如此 - Explicit call to destructor of template parameter type, even when instantiated on a builtin 相同方法签名的模板化和显式参数类型版本 - Templated and explicit parameter type versions of same method signature 将一种方法作为参数传递给另一种方法的可能性 - Possibility of passing a method as a parameter to another method 在异步调用中将shared_ptr作为参数传递 - Passing shared_ptr as parameter in async call 将函数作为参数传递给C ++中的方法 - Passing a function as a parameter to a method in C++ 将函数作为参数传递给模板类的模板方法 - passing function as parameter to template method of template class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM