简体   繁体   English

C 和 function 指针中的 typedef

[英]typedef in C and function pointers

I have some structures and corresponding functions that operate on them written in their own header and c file.我有一些结构和相应的函数可以在他们自己的 header 和 c 文件中编写。

I wondered if it was possible to create a new header and c file for code that would "inherit" those particular types and functions with a new appropriatly descriptive declaration?我想知道是否有可能创建一个新的 header 和 c 文件,用于使用新的适当描述性声明“继承”这些特定类型和函数的代码? Is it possible to typedef functions and structs in this manner in order to reuse the code?是否可以以这种方式对函数和结构进行 typedef 以重用代码?

I have looked into function pointers but I am not sure if this is the correct tool to achieve what I am after.我研究了 function 指针,但我不确定这是否是实现我所追求的正确工具。 I guess one other option is to refactor the code so that the names are generic.我想另一种选择是重构代码,以便名称是通用的。

Code example:代码示例:

// function1.h

typedef struct src_data {
      ...
} src_data;

src_data* process_src_data(...) {
   ...
   return new_data;
}
// function2.h

#include "function1.h"
typedef src_data dest_data;

typedef dest_data* (*process_dest)(void);
process_dest process_dest_data = &process_src_data;

usage would then be as follows:用法如下:

#include "function1.h"
#include "function2.h"

src_data *sourceData = process_src_data(...);

dest_data *destinationData = process_dest_data(...); 

A crude and simple solution:一个粗略而简单的解决方案:

// function2.h
#define process_dest_data process_src_data

A slightly nicer approach is using static inline functions as wrappers:更好的方法是使用static inline函数作为包装器:

// function2.h
static inline dest_data* process_dest_data(void) {
    return process_src_data();
}

Using inline will help you avoid warnings about non-used functions.使用inline将帮助您避免有关未使用函数的警告。

Both will likely result is similar object code.两者都可能导致类似的 object 代码。

There is a subtle advantage of a macro over a wrapper.与包装器相比,宏有一个微妙的优势。 Assume that process_src_data has external linkage.假设process_src_data有外部链接。 Expression &process_dest_data may have a different value in each translation unit.表达式&process_dest_data在每个翻译单元中可能具有不同的值。 If macro were use then the value would be the same, equal to &process_src_data .如果使用宏,则值将相同,等于&process_src_data

if it was possible to create a new header and c file for code that would "inherit" those particular types and functions with a new appropriatly descriptive declaration?如果可以为代码创建一个新的 header 和 c 文件,以使用新的适当描述性声明“继承”这些特定类型和函数?

C is a very (nowadays;) simple language that you have to write a lot yourself. C 是一种非常(现在)简单的语言,您必须自己编写很多代码。 Other language - notably thinking of object-oriented languages, C++, Java - have a builtin "inheritance" feature that the language, by itself, allows to "import" all function from one place to another.其他语言 - 特别是考虑面向对象的语言,C++,Java - 具有内置的“继承”功能,该语言本身允许将所有 ZC1C425268E687A94D1AB5074C1 从一个地方“导入”到另一个地方。

C does not have this feature. C 没有此功能。 In C, you have to write it all, from the top yourself.在 C 中,您必须自己从头开始编写所有内容。

I am not sure if this is the correct我不确定这是否正确

  1. Do not define functions in a header file.不要在 header 文件中定义函数。 Do not:不要:

// function1.h
src_data* process_src_data(void) { /* NO */ }

instead put a funciton declaration in a header and definition in a source file.而是将功能声明放在 header 和源文件中的定义中。

// function1.h
src_data* process_src_data(void); //ok
// function1.c
src_data* process_src_data(void) { ok(); }

otherwise it's not "correct", in a sense multiple .c files that include that header linked together will cause "multiple deifnitions" problems.否则它不是“正确的”,从某种意义上说,包含链接在一起的 header 的多个.c文件将导致“多个定义”问题。

  1. Similar with function pointer declaration:与 function 指针声明类似:

// function2.h
process_dest process_dest_data = &process_src_data;

Either make the function pointer static , or move it to separate C source file and add extern to the header. Either make the function pointer static , or move it to separate C source file and add extern to the header. Additionally add const to it if it's intended to be constant, so it can be optimized to read-only section and not use RAM.如果它打算保持不变,另外添加const ,因此可以将其优化为只读部分而不使用 RAM。

// function2.h
extern const process_dest process_dest_data;
// function2.c 
const process_dest process_dest_data = &process_src_data;

tool to achieve what I am after.实现我所追求的工具。

Function pointers take memory. Function 指针取 memory。 Calling a function pointers needs to dereference them.调用 function 指针需要取消引用它们。 They are hard to optimize.它们很难优化。

It's typical in C to write short static wrapper functions:在 C 中编写简短的static包装函数是典型的:

// function2.h
static process_dest process_dest_data(void) {
     return process_src_data();
}

If the function is short, it will be inlined by the compiler and removed from the resulting executable.如果 function 很短,它会被编译器内联并从生成的可执行文件中删除。 For longer, just write a regular function that calls the underlying implementation.更长时间,只需编写一个调用底层实现的常规 function 即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM