我们有很多看起来像这样的重复代码:
void WriteA(A *to, A *from);
void WriteB(B *to, B *from);
void WriteC(C *to, C *from);
void WriteA(A *to, A *from){
*to = *from;
}
void WriteB(B *to, B *from){
*to = *from;
}
void WriteC(C *to, C *from){
*to = *from;
}
尽管 C 没有函数重载或模板,但我还是想将它变成一个通用函数。我想我在这里唯一的选择是使用 void 指针。
void Write(void *to, void *from);
但是,似乎没有办法在函数内部导出类型:
void Write(void *to, void *from){
*to = *from
}
编译器错误:非法类型:void '=' void
似乎也没有办法传递第三个参数,它是一个类型名(同样,我认为只有 C++ 可以用模板做的事情)。
我将如何着手编写这种函数?如果可能的话,我想避免使用预处理器。
我想我也许可以这样做:
void Write(void *to, void *from){
memcpy(pTo, pFrom, sizeof(*pTo));
}
但是,
编译器错误:不知道对象的大小
最佳答案
如果您依赖于 struct
成员,一种方法是使用 C 预处理器。
但是根据您的评论,您实际上只有一个依赖于 struct
类型和成员,这是在特定位置完成的分配。
最简单的方法是在上面的评论中使用 Leonardo Herrera 的建议并传入 struct
大小,然后使用 memcpy()
与 struct
参数的大小。
另一种可能性是创建两个函数,一个在赋值之前完成工作,另一个在赋值之后完成工作,然后调用第一个函数,进行实际赋值,然后调用第二个函数。例如:
// define the structs that we are using.
typedef struct {
int jj;
int kk;
} s1;
typedef struct {
int kk;
int ii;
int jj;
} s2;
// define the function that does the first part of the work
void PhaseOne (/* arg list */)
{
// phase one of the functionality
}
// define the function that does the second part of the work
void PhaseTwo (/* arg list */)
{
// phase two of the functionality
}
myFunctionUser (void)
{
s1 a1, b1;
s2 a2, b2;
// do things with a1 and b1 in prep to call the function
// do things with a2 and b2 in prep to call the function
PhaseOne (/* arg list */);
a1 = b1;
PhaseTwo (/* arg list */);
PhaseOne (/* arg list */);
a2 = b2;
PhaseTwo (/* arg list */);
// do more stuff
}
如果需要在 PhaseOne()
和 PhaseTwo()
之间传递状态,或者如果需要对基于PhaseOne()
或 PhaseTwo()
的状态,那么您可能需要一个 struct
或其他变量类型。
然而,另一件事是编写一个函数,该函数接受指向另一个函数的指针,并在另一个函数中进行赋值。这是借用了 C 标准库如何使用带有 qsort()
函数的比较函数的想法。使用这种方法,如果除了赋值之外还有其他与结构有关的事情,那么指向的函数将是执行此操作的地方,因为此时您可以转换为适当的类型,然后执行任何操作。
// define the structs that we are using.
typedef struct {
int jj;
int kk;
} s1;
typedef struct {
int kk;
int ii;
int jj;
} s2;
void AssignS1 (void *a, void *b) { *((s1 *)a) = *((s1 *)b); return; }
void AssignS2 (void *a, void *b) { *((s2 *)a) = *((s2 *)b); return; }
void myFunction (void *a, void *b, void (*pFunc)(void *x, void *y))
{
// do stuff
pFunc(a, b); // do the assignment
// do more and more and tons of stuff
}
myFunctionUser (void)
{
s1 a1, b1;
s2 a2, b2;
// do things with a1 and b1 in prep to call the function
// do things with a2 and b2 in prep to call the function
myFunction (&a1, &b1, AssignS1);
myFunction (&a2, &b2, AssignS2);
// do more stuff
}
这里仅供引用,这是一种使用 C 预处理器根据特定结构类型生成源代码的方法。它非常丑陋且难以调试,并且需要程序员一定程度的纪律。这是一个用于说明过程的小而简单的函数,我向您保证它不会很好地扩展。
// define a macro that will generate various type specific versions of a function.
#define WriteThing(s) void WriteThing##s (s *a, s *b) \
{ *a = *b; if (a->jj != 4) { a->jj += b->jj + 10;} return; }
// define the function we will use to call the above function based on the type of the arguments
#define WriteThingCall(s,a,b) WriteThing##s (a, b)
// define the structs that we are using.
typedef struct {
int jj;
int kk;
} s1;
typedef struct {
int kk;
int ii;
int jj;
} s2;
// generate the functions we are going to need for our different types
WriteThing(s1)
WriteThing(s2)
// example function of how to use this approach.
void func (void)
{
s1 a1 = {0}, b1 = {0};
s2 a2 = {0}, b2 = {0};
WriteThingCall(s1, &a1, &b1);
WriteThingCall(s2, &a2, &b2);
}
关于c - 是否可以在 C 中编写与类型无关的函数,并在实现中派生类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30104344/