标签: function-pointers

成员函数指针调用复制构造函数?

我正在尝试在我的代码中创建成员函数的查找表,但它似乎试图调用我的复制构造函数,我通过扩展"uncopyable"类来阻止它.我所拥有的是以下内容.

enum {FUN1_IDX, FUN2_IDX, ..., NUM_FUNS };

class Foo {
  fun1(Bar b){ ... }
  fun2(Bar b){ ... }
  ...
  void (Foo::*lookup_table[NUM_FUNS])(Bar b);
  Foo(){ 
    lookup_table[FUN1_IDX] = &Foo::fun1;
    lookup_table[FUN2_IDX] = &Foo::fun2;
  }

  void doLookup(int fun_num, Bar b) {
    (this->*lookup_table[fun_num])(b);
  }
};
Run Code Online (Sandbox Code Playgroud)

错误是'(this - > ...'行试图调用复制构造函数,这是不可见的.为什么要尝试这样做,我需要改变什么,所以它不会?

c++ function-pointers lookup-tables

0
推荐指数
1
解决办法
606
查看次数

从成员函数模板化参数调用成员函数

鉴于以下代码,我无法编译.

    template < typename OT, typename KT, KT (OT::* KM)() const >
    class X
    {
    public:
        KT mfn( const OT & obj )
        {
            return obj.*(KM)();    // Error here.
        }
    };

    class O
    {
    public:
        int func() const
        {
            return 3;
        }
    };

    int main( int c, char *v[] )
    {
        int a = 100;

        X<  O, int, &O::func > x;

        O o;

        std::cout << x.mfn( o ) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

我收到了folling错误消息

error: must use '.*' or '->*' to call …
Run Code Online (Sandbox Code Playgroud)

c++ templates member-function-pointers function-pointers pointer-to-member

0
推荐指数
1
解决办法
295
查看次数

程序的哪个部分是存储的函数指针?

我想知道程序的哪个部分是存储的函数指针?在,它是在程序堆栈上还是有一个单独的部分?

void f(void){}
int main(void){
 int x[10];
 void (*fp)(void) = NULL;
 fp = f;
 return 0;
}
Run Code Online (Sandbox Code Playgroud)

现在,x和fp的地址是否在程序堆栈内存的同一段中?

c function-pointers function

0
推荐指数
1
解决办法
330
查看次数

ANSI C - 作为其他函数的参数的函数

我有这些原型:

int *my_func(int x, void (*other_func)(int a, int b));

int func2(int val1, int val2);
Run Code Online (Sandbox Code Playgroud)

假设有一个匹配它的函数.

如果我想实际调用my_func,我该怎么做?我试过以下没有运气:

my_func(1,func2);
Run Code Online (Sandbox Code Playgroud)

这是错误:

warning: passing argument 2 of ‘my_func’ from incompatible pointer type
Run Code Online (Sandbox Code Playgroud)

c function-pointers ansi

0
推荐指数
1
解决办法
2433
查看次数

为什么这个程序中printf的使用有误?

我在回答C中的函数指针如何工作时看到了这个示例代码?

#import <stdlib.h>
#define MAX_COLORS  256

typedef struct {
    char* name;
    int red;
    int green;
    int blue;
} Color;

Color Colors[MAX_COLORS];


void eachColor (void (*fp)(Color *c)) {
    int i;
    for (i=0; i<MAX_COLORS; i++)
        (*fp)(&Colors[i]);
}

void printColor(Color* c) {
    if (c->name)
        printf("%s = %i,%i,%i\n", c->name, c->red, c->green, c->blue);
}

int main() {
    Colors[0].name="red";
    Colors[0].red=255;
    Colors[1].name="blue";
    Colors[1].blue=255;
    Colors[2].name="black";

    eachColor(printColor);
}
Run Code Online (Sandbox Code Playgroud)

该代码返回以下错误:

test.c: In function ‘printColor’:
test.c:21: warning: incompatible implicit declaration of built-in function ‘printf’
Run Code Online (Sandbox Code Playgroud)

c function-pointers

0
推荐指数
1
解决办法
136
查看次数

"事件"和成员函数指针的C++映射

我已经设法编写一个模板类来像回调一样工作,从这个问题的接受答案中学习如何定义一般成员函数指针.

我希望有一个字符串键和回调值的映射,以便我可以调用匹配字符串的正确回调.这没关系,但我需要地图来支持来自不同类的回调.现在它只能用于一个类.它可以是任何类,因为模板只是来自同一个类的回调集合.

class Apple {
public:
    void Red () {
        cout << "aa";
    }
};

class Orange {
public:
    void Blue () {
        cout << "bb";
    }
};

template <typename T>
class Callback {
    T *obj;
    void (T::*ptr) (void);

public:
    Callback (T *obj, void (T::*ptr) (void)) : obj (obj), ptr (ptr) {

    }

    void Call () {
        (obj->*ptr) ();
    }
};
Run Code Online (Sandbox Code Playgroud)

我可以这样使用它

Apple apple;
Orange orange;
Callback <Apple> callA (&apple, &Apple::Red);
Callback <Orange> callB (&orange, &Orange::Blue);
callA.call (); …
Run Code Online (Sandbox Code Playgroud)

c++ templates member-function-pointers function-pointers callback

0
推荐指数
2
解决办法
2458
查看次数

指向成员函数的向量

我试图写它创建一个包含类的程序矢量指向成员函数,与add()和remove()成员函数.我写的代码是 -

1    #include <iostream>
2    #include <vector>
3   using namespace std;
4    
5    typedef void(*classFuncPtr)();
6    
7    class FunctionVectors
8    {
9    private:
10      vector<classFuncPtr> FunctionPointerVector;
11    public:
12      FunctionVectors(){}
13      void add(classFuncPtr funcPtr);
14      void remove(int index);
15      void run();
16      void a(){cout<<"a: Why are you calling me?"<<endl;}
17    };
18    
19    void FunctionVectors::add(classFuncPtr funcPtr)
20    {
21      FunctionPointerVector.push_back(funcPtr);
22    }
23    
24    void FunctionVectors::remove(int index)
25    {
26      FunctionPointerVector.erase(FunctionPointerVector.begin() + index); …
Run Code Online (Sandbox Code Playgroud)

c++ function-pointers vector

0
推荐指数
1
解决办法
3874
查看次数

作为参数传递的打印功能指针导致在屏幕上打印"1"

我一直在尝试使用函数指针,发现以下程序的行为相当神秘:

void foo(int(*p)())
{ std::cout << p << std::endl; }

int alwaysReturns6()
{ return 6; }

int main()
{
    foo(alwaysReturns6);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

上面的代码在屏幕上打印数字"1".

我知道我应该像这样访问函数指针:( p()然后打印6),但是我仍然没有得到函数中使用的plain p或*pmeans foo.

c++ function-pointers

0
推荐指数
1
解决办法
39
查看次数

如何将void*转换为函数指针?

我在FreeRTOS中使用xTaskCreate,其第四个参数(void*const)是传递给新线程调用的函数的参数.

void __connect_to_foo(void * const task_params) {
   void (*on_connected)(void);
   on_connected = (void) (*task_params);
   on_connected();
}

void connect_to_foo(void (*on_connected)(void)) {
   // Start thread
   xTaskCreate(
         &__connect_to_foo,
         "ConnectTask",
         STACK_SIZE,
         (void*) on_connected, // params
         TASK_PRIORITY,
         NULL // Handle to the created Task - we don't need it.
         );
}
Run Code Online (Sandbox Code Playgroud)

我需要能够传入一个带签名的函数指针

void bar();

但我无法弄清楚如何将void*转换为我可以调用的函数指针.我能得到的最近的是:

错误:'void*'不是第3行的指针对象类型

如何将task_params转换为我可以调用的函数指针?

注意,上面的代码大大简化了.

c++ function-pointers freertos

0
推荐指数
1
解决办法
752
查看次数

C:如何访问存储在void指针中的函数指针(void*)?

混淆了如何访问存储在void指针(void*)中的函数指针.


假设你有这个:

void *functions[] =
{
    &sqrt,         // int ft_sqrt(int nb);
    &power,
    &logN,
    &factorial;
};

// An array of void pointers, each storing a function pointer.
Run Code Online (Sandbox Code Playgroud)

如果我想访问该sqrt功能,我的猜测如下:

(int (*)(int)) functions[0](x)

但我的猜测是错误的:

error: called object type 'void *' is not a function or function pointer


那么如何访问其中一个函数呢?

c pointers function-pointers void-pointers typecasting-operator

0
推荐指数
2
解决办法
243
查看次数