Assign a pointer to a function address of a pointer to a function object

Is this possible in C ++? For example, I have a pointer to a function that takes no parameters, and its return type is invalid:

void (*f)();

and and function object:

class A
{
public:
    void operator()() { cout << "functor\n"; }
};

Can I assign fan object address A? And when do I call f()to call a functor A?

I tried this, but it does not work:

#include <iostream>
using namespace std;

class A
{
public:
    void operator()() { cout << "functorA\n"; }
};

int main()
{
    A ob;
    ob();
    void (*f)();
    f = &ob;
    f();       // Call ob();
    return 0;
}

I get C:\Users\iuliuh\QtTests\functor_test\main.cpp:15: error: C2440: '=' : cannot convert from 'A *' to 'void (__cdecl *)(void)' There is no context in which this conversion is possible

Is there any way to achieve this?

+5
source share
4 answers

You cannot do this as you indicated, because:

  • operator () must be a non-static function (standard requirement)
  • a pointer to a non-stationary function must have an implicit parameter - a pointer to an instance of the class
  • f() A

++ 11 std:: function, , - :

std::function<void(void)> f = std::bind(&A::operator(), &ob);

(. question std:: -)

+5

++ 11, std:: function

#include <functional>

std::function<void()> f;

int main()
{
    A ob;
    ob();

    f = ob;    // f refers to ob
    f();       // Call ob();
    return 0;
}
+3

, ++ 1/++ 0x, std:: function, , .

#include <functional>

 class A
{
public:
    void operator()() { }
};

int main()
{
    std::function<void(void)> aFunction;
    A ob;

    aFunction = ob;

// or as another user said
// aFunction = std::bind(&A:operator(), &ob);

    aFunction();

    void (*f)();

    aFunction = f;

    aFunction();

    return 0;
}

++ 03, std::mem_fun std::ptr_fun

+2

:

Basically, you want to have a common way to call member functions and functions. Then perhaps you can create a wrapper that will represent a generic pointer to a function or a member function. Say you have a class Baseand want to be able to call operator()all derived classes. Then you also have function()which you want to call:

class Base
{
public:
    virtual void operator()() = 0;
};

class A : public Base
{
public:
    void operator()(){ std::cout << "A()" << std::endl; }
};

void function()
{
    std::cout << "function" << std::endl; 
}

If you create a wrapper that allows you to create your own pointer ( MyFncPtr):

typedef void (Base::*BaseFncPtr)();
typedef void (*FncPtr)();

class MyFncPtr
{
public:
    MyFncPtr(FncPtr f) : fnc(f), baseObj(NULL), baseFnc(NULL) { }
    MyFncPtr(BaseFncPtr fPtr, Base* objPtr) : baseFnc(fPtr), baseObj(objPtr), fnc(NULL)  { }
    void invoke()
    {
        if (baseObj && baseFnc)
            (baseObj->*baseFnc)();
        else if (fnc)
            fnc();
    }

private:
    BaseFncPtr baseFnc;
    Base* baseObj;
    FncPtr fnc;
};

you could achieve this as follows:

A a;
MyFncPtr myPtr(&Base::operator(), &a);
myPtr.invoke();
MyFncPtr myPtr2(function);
myPtr2.invoke();

outputs:

A()
function

Hope this helps :)

+1
source

All Articles