我有四个文件f1,f2,f3和f4具有相同的功能名称和签名但实现不同.
f1 :
func1(int par1,int par2)
f2 :
func1(int par1,int par2)
f3 :
func1(int par1,int par2)
f4 :
func1(int par1,int par2)
Run Code Online (Sandbox Code Playgroud)
现在将根据某个版本ID调用每个函数,例如,如果版本ID为1,我将调用文件1的func1,如果它是2,我将调用文件2的func1.如何实现它!
我尝试创建另一个函数参数作为版本ID,但后来我必须更改所有不可接受的函数签名.这必须在C中完成.如果它在C++中我可以创建一个类并且可以放置每个文件内容在一个新类中然后创建每个类的实例但它的纯C.
或者是否有相同的#Pragma!
任何输入!
你真的有两个不同的问题要处理.
首先是这些函数都具有相同的名称,因此您无法明确地引用它们中的任何一个.要解决这个问题,你几乎肯定想将它们重命名为func1,func2,func3等.
然后你必须得到一个输入1到呼叫func2,一个输入2到呼叫func2,等等.幸运的是,这很容易管理:
// the type of a pointer to one of the functions:
typedef void func(int par1, int par2);
// an array of pointers to the functions:
func funcs[] = {func1, func2, func3, func4};
// call the correct function from the array, based on the ID:
funcs[ID]();
Run Code Online (Sandbox Code Playgroud)