如何重载C中的函数? [英] How to overload functions in C?

查看:52
本文介绍了如何重载C中的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于此主题的文章很多,但是所有这些文章都不适合C编程的初学者.

There are lots of posts on this topic, but all of them are not for a complete beginners in C programming.

问题是我具有三个功能:

The thing is that I have three functions:

struct model* createModel_empty(void);
struct model* createModel_single_value(double value);
struct model* createModel_single_value(const double* arr_value);

我只想在我的代码中使用createModel函数,它将被正确的实现替换.

And I want to just use createModel function in my code which will be substituted with the right implementation.

根据我在Internet上的研究,尤其是堆栈溢出,我了解有两种方法可以执行类似的操作:

From my research over the Internet and specifically stack overflow I understood that there are two options to do something like that:

  1. 将宏与_Generics和& __ARGS_V__(没有实现IDEA的方法)
  2. 使用带下划线参数的宏,例如foo(_1,_2,_3...),但我真的不明白那些下划线的含义
  1. Use macros with _Generics & __ARGS_V__ (no IDEA how to implement)
  2. Use macros with underscored parameters like foo(_1,_2,_3...), but I really don't get what those underscore mean

请帮助我解决这个问题;(

Please help me with this issue ;(

推荐答案

关于此主题的文章很多,但是所有这些文章都不适合C编程的初学者.

There are lots of post on this topic, but all of them are not for a complete beginners in C programming

仅因为这些技术不适合初学者.出于相同的原因,您不会找到针对不能玩三个球的人的七球玩法教程.

Simply because these techniques are not suitable for beginners. For the same reason, you would not find a tutorial for juggling with 7 balls that is aimed at people who cannot juggle with three.

但是有一个相当容易的方法-尽管体积很大-解决方法.将数据包装在结构中.

But there is a fairly easy - although bulky - workaround. Wrap the data in a struct.

struct data {
    union {
        const double *p;
        double d;
    } data;
    int type;
};

struct model* createModel_single_value(struct data data) {
    switch(data.type) {
        case 0: return createModel_single_value_double(data.data.d);
        case 1: return createModel_single_value_const_double_ptr(data.data.p);
        default: return NULL;
    }
}

请注意,这是一个运行时解决方案,而不是您使用上述方法可以实现的编译时间.这会使它易于出现有趣的行为".如果您不小心,那么我建议您采取以下预防措施:

Note that this is a runtime solution and not a compile time which you can achieve with the methods you mentioned. This can make it prone to "interesting behavior" if you're not careful, so I would recommend these precautions:

使用enum作为类型,唯一的原因是它更具可读性:

Use an enum for type, for the sole reason that it's more readable:

enum type { DOUBLE, CONSTDOUBLEPTR };

struct data {
    enum type type;
    ...

并在工作功能中添加断言:

And add asserts in the working functions:

struct model* createModel_single_value_double(struct data data) {
    assert(data.type == DOUBLE);

如果这些确实是性能问题,则可以稍后将其删除.很有可能不是.

If these really are a performance problem, you can remove them later. Most likely, they are not.

但是,总的来说,我建议您选择一种支持所需功能的语言,并在选择语言后按预期使用.不要试图用它不适合的东西来拔毛.

But in general, I would give the advice that you should choose a language that supports the features that you need, and when you have chosen a language, use it as intended. Don't try to shoehorn in stuff that it was not designed for.

滥用语言确实很有趣,它也可以帮助您获得很多见识.但是这些技术很少有用.但是,了解它们的工作原理通常可以在调试中提供很大的帮助.

Abusing a language can be very fun indeed, and it can also help you get a lot of insights. But these techniques are rarely very useful. Understanding how they work can however often be a great help in debugging.

这篇关于如何重载C中的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆