C 两个函数合二为一

标签 c casting

我有两个函数做完全相同的事情,但是在两种不同类型的结构中,这两种类型的结构非常相似。

假设我有这两个结构。

typedef struct nodeOne{
    Date *date;
    struct nodeOne *next;
    struct nodeOne *prev;
}NodeOne;

typedef struct nodeTwo{
    Date *date;
    struct nodeTwo *next;
    struct nodeTwo *prev;
}NodeTwo;

因为我销毁每个列表的函数几乎相同(只是参数的类型不同)我只想创建一个函数来使两个变薄。

我有这两个功能

void destroyListOne(NodeOne **head, NodeOne **tail){
    NodeOne *aux;

    while (*head != NULL){
        aux = *head;
        *head = (*head)->next;
        free(aux);
    }
    *tail = NULL;
}

还有这个:

void destroyListTwo(NodeTwo **head, NodeTwo **tail){
    NodeTwo *aux;

    while (*head != NULL){
        aux = *head;
        *head = (*head)->next;
        free(aux);
    }
    *tail = NULL;
}

因为它们非常相似,所以我想做这样的东西:

void destroyList(void **ini, void **end, int listType){

    if (listType == 0) {
        NodeOne *aux;
        NodeOne head = (NodeOne) ini;
        NodeOne tail = (NodeOne) ed;

    }
    else {
        NodeTwo *aux;
        NodeTwo head = (NodeTwo) ini;
        NodeTwo tail = (NodeTwo) ed;
    }

    while (*head != NULL){
        aux = *head;
        *head = (*head)->next;
        free(aux);
    }
    *tail = NULL;
}

正如您现在所见,这行不通,但我想知道这是否有可能实现。

我必须按原样维护这两个结构。

最佳答案

正如@Dancrumb 所指出的,这里存在一些设计问题,我不建议您按照您的意愿去做。

也就是说,转换可以完成,前提是nodeOnenodeTwo 总是将成为相同(我在生产代码中永远不会依赖的东西)。

你可以只选择一个,然后一直施放它(不寒而栗)。由于它们是具有不同名称的相同结构,因此转换有效:

void destroyList(void *ini, void *end, int listType){

    NodeOne *aux = NULL;
    NodeOne **head = ini;
    NodeOne **tail = end;

    while (*head != NULL){
        aux = *head;
        *head = (*head)->next;
        free(aux);
    }
    *tail = NULL;
}

另请注意,在 C 中,您不需要显式强制转换,因为 void * 可以隐式转换为任何其他指针类型而无需强制转换。

但是说真的,请不要这样做。它脆弱、不可维护且容易出错。


看完@Torp 的回答后,我想详细说明问题的精神和我的回答。通过对@Torp 代码的错误修复(它无法编译并且存在几个指针问题),它可以正常工作。也就是说,我仍然认为您不应该让它发挥作用。

特别是当我们谈论 C(而不是 C++)时,我肯定会为单独的列表类型保留销毁函数。我尽量避免剪切和粘贴代码,但在这种情况下,我认为安全、清晰和可维护性是最重要的。我的意见,当然。你的里程可能会有所不同:)

关于C 两个函数合二为一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10918078/

相关文章:

c - 如何在C99中定义一个函数为inline internal and external copy

c - 尝试从 C 连接到 postgres 时出现问题

c - 无论返回码如何,都在循环中释放内存

C# 将位图转换为 FFmediaToolkits ImageData

c++ - 标准库标签

c++ - 在 OpenGL 中绘制形状的标准(常见)方法是什么?

java - 未经检查的类型转换 : Wildcard wrapped by Class to another type wrapped by Class

c++ - 在 C++ 中解析字符串

Java - 你如何向上和向下转换同一个对象

c++ - 错误 : Conversion from integral type to pointer type requires reinterpret_cast, C 风格转换或函数风格转换