我正在尝试在我的代码中创建成员函数的查找表,但它似乎试图调用我的复制构造函数,我通过扩展"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 - > ...'行试图调用复制构造函数,这是不可见的.为什么要尝试这样做,我需要改变什么,所以它不会?
鉴于以下代码,我无法编译.
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
我想知道程序的哪个部分是存储的函数指针?在,它是在程序堆栈上还是有一个单独的部分?
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的地址是否在程序堆栈内存的同一段中?
我有这些原型:
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中的函数指针如何工作时看到了这个示例代码?
#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) 我已经设法编写一个模板类来像回调一样工作,从这个问题的接受答案中学习如何定义一般成员函数指针.
我希望有一个字符串键和回调值的映射,以便我可以调用匹配字符串的正确回调.这没关系,但我需要地图来支持来自不同类的回调.现在它只能用于一个类.它可以是任何类,因为模板只是来自同一个类的回调集合.
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
我试图写它创建一个包含类的程序矢量指向成员函数,与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) 我一直在尝试使用函数指针,发现以下程序的行为相当神秘:
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.
我在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转换为我可以调用的函数指针?
注意,上面的代码大大简化了.
混淆了如何访问存储在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