我可以把一个函数放在一个结构中吗?

时间:2022-11-23 11:32:07

Maybe I use inline functions...
There are any way to archieve it, without using function pointers?

也许我用内联函数……有什么方法可以不使用函数指针来归档它?

Thanks in advance.

提前谢谢。

5 个解决方案

#1


5  

Not in C. You can in C++. A struct is just a class with all members being public by default in that language.

不是C,你可以用c++。struct只是一个类,所有成员在该语言中默认都是公共的。

#2


4  

No, structure supports only a pointer to a function.

不,结构只支持指向函数的指针。

#3


3  

Not in C. But you can in C++.

不是C语言,但是你可以用c++。

#4


1  

No

没有

The only way I can think of is similar to this one (with function pointers).

我能想到的唯一方法是类似于这个方法(使用函数指针)。

#5


1  

to complement all other answers, i would add that a, in C, a structure is only for storing data: a function is not data, so it cannot be stored into a structure.

为了补充所有其他答案,我要补充一点,在C语言中,结构仅用于存储数据:函数不是数据,因此不能存储到结构中。

however, as pcent pointed out, a pointer to a function is a type of data and can be stored in a structure.

但是,正如pcent指出的,指向函数的指针是一种数据类型,可以存储在结构中。

#1


5  

Not in C. You can in C++. A struct is just a class with all members being public by default in that language.

不是C,你可以用c++。struct只是一个类,所有成员在该语言中默认都是公共的。

#2


4  

No, structure supports only a pointer to a function.

不,结构只支持指向函数的指针。

#3


3  

Not in C. But you can in C++.

不是C语言,但是你可以用c++。

#4


1  

No

没有

The only way I can think of is similar to this one (with function pointers).

我能想到的唯一方法是类似于这个方法(使用函数指针)。

#5


1  

to complement all other answers, i would add that a, in C, a structure is only for storing data: a function is not data, so it cannot be stored into a structure.

为了补充所有其他答案,我要补充一点,在C语言中,结构仅用于存储数据:函数不是数据,因此不能存储到结构中。

however, as pcent pointed out, a pointer to a function is a type of data and can be stored in a structure.

但是,正如pcent指出的,指向函数的指针是一种数据类型,可以存储在结构中。