user1733773
user1733773

Reputation: 360

Explicit passing "this" parameter to method call

Is it possible in c++ call class method with explicit passing first "this" param to it?

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:

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);

Upvotes: 6

Views: 380

Answers (2)

Jarod42
Jarod42

Reputation: 217145

Do you want something like the following ?

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

..

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

Upvotes: 0

Here is an idea for a dispatcher which you could use inside your 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)...); }

Upvotes: 5

Related Questions