C++ “typedef void (*Something)()”是什么意思
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3982470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does "typedef void (*Something)()" mean
提问by DogDog
I am trying to understand what this means, the code I am looking at has
我试图理解这意味着什么,我正在查看的代码有
in .h
在.h
typedef void (*MCB)();
static MCB m_process;
in .C
在.C
MCB Modes::m_process = NULL;
And sometimes when I do
有时当我这样做时
m_process();
I get segmentations fault, it's probably because the memory was freed, how can I debug when it gets freed?
我得到了分段错误,这可能是因为内存被释放了,当它被释放时我该如何调试?
I hope my questions are clear.
我希望我的问题很清楚。
回答by Jonathan Leffler
It defines a pointer-to-function type. The functions return void, and the argument list is unspecified because the question is (currently, but possibly erroneously) tagged C; if it were tagged C++, then the function would take no arguments at all. To make it a function that takes no arguments (in C), you'd use:
它定义了一个指向函数的指针类型。函数返回 void,参数列表未指定,因为问题是(当前,但可能错误地)标记为 C;如果它被标记为 C++,那么该函数将根本不接受任何参数。要使其成为不带参数的函数(在 C 中),您可以使用:
typedef void (*MCB)(void);
This is one of the areas where there is a significant difference between C, which does not - yet - require all functions to be prototyped before being defined or used, and C++, which does.
这是 C 之间存在显着差异的领域之一,C 并不 - 还 - 要求在定义或使用之前对所有函数进行原型设计,而 C++ 则需要。
回答by Jim Brissom
It introduces a function pointer type, pointing to a function returning nothing (void), not taking any parameters and naming the new type MCB.
它引入了一个函数指针类型,指向一个不返回任何内容(void)的函数,不带任何参数并命名新类型MCB。
回答by Oliver Charlesworth
The typedef defines MCB
as the type of a pointer to a function that takes no arguments, and returns void
.
typedef 定义MCB
为指向不带参数的函数的指针的类型,并返回void
.
Note that MCB Modes::m_process = NULL;
is C++, not C. Also, in C, the typedef should really be typedef void (*MCB)(void);
.
请注意,这MCB Modes::m_process = NULL;
是 C++,而不是 C。此外,在 C 中,typedef 应该真的是typedef void (*MCB)(void);
.
I'm not sure what you mean by "the memory was freed". You have a static pointer to a function; a function cannot be freed. At most, your pointer has been reset somewhere. Just debug with a memory watch on m_process
.
我不确定您所说的“内存已释放”是什么意思。你有一个指向函数的静态指针;一个函数不能被释放。最多,您的指针已在某处重置。只需使用内存手表进行调试即可m_process
。
回答by Pablo Santa Cruz
It's a function pointer. You get a SEGMENTATION FAULT because you are trying to make a call to a function which address is invalid (NULL
).
它是一个函数指针。您收到 SEGMENTATION FAULT,因为您试图调用地址无效的函数 ( NULL
)。
According to your specific sample, the function should return no value (void
) and should receive no parameters ()
.
根据您的具体示例,该函数不应返回值 ( void
) 且不应接收任何参数()
。
This should work:
这应该有效:
void a()
{
printf("Hello!");
}
int main(int arcg, char** argv)
{
m_process = a;
m_process(); /* indirect call to "a" function, */
// Hello!
}
Function pointers are commonly used for some form of event handling in C. It's not its only use though...
函数指针通常用于 C 中的某种形式的事件处理。虽然它不是它唯一的用途......
回答by Krishna Kanth Yenumula
Let's take an example
让我们举个例子
typedef void (*pt2fn)(int);
Here, we are defining a type pt2fn. Variables of this type point to functions, that take an integer as argument and does not return any value.
在这里,我们定义了一个类型 pt2fn。这种类型的变量指向函数,它接受一个整数作为参数并且不返回任何值。
pt2fn kk;
Here, kk is a variable of type pt2fn, which can point to any function that takes in an integer as input and does not return any value.
这里,kk 是一个 pt2fn 类型的变量,它可以指向任何接受整数作为输入并且不返回任何值的函数。
Reference:https://cs.nyu.edu/courses/spring12/CSCI-GA.3033-014/Assignment1/function_pointers.html
参考:https: //cs.nyu.edu/courses/spring12/CSCI-GA.3033-014/Assignment1/function_pointers.html