C中重载的函数 [英] overloaded functions in C

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

问题描述




在C中实现重载功能的最佳方法是什么?对于

实例,如果你想拥有各种

结构的通用打印功能,我的实现将是一个案例陈述:


void print_structs(void * struct_argument,enum struct_type stype)

{

switch(stype){

case STRUCT_TYPE_1:

struct type1 * p =(struct type1 *)struct_argument;

printf(" type1.field1:%d \ n",type1.field1);

案例STRUCT_TYPE_2:

...

}

返回;

}


有一点我不喜欢这个,你必须在枚举中编码类型

信息

。我不喜欢这个,因为我不喜欢将用户定义的方面编码为一般的代码。有没有更好的方式来做这个呢?


谢谢,

Bahadir

Hi,

What''s the best way to implement an overloaded function in C? For
instance if you want to have generic print function for various
structures, my implementation would be with a case statement:

void print_structs(void * struct_argument, enum struct_type stype)
{
switch(stype) {
case STRUCT_TYPE_1:
struct type1 * p = (struct type1 *) struct_argument;
printf("type1.field1: %d\n", type1.field1);

case STRUCT_TYPE_2:
...
}
return;
}

One thing I don''t like about this is that, you must encode type
information
in an enum. I don''t like this because I don''t prefer encoding
user-defined aspects into code in general. Is there a better way of
doing this?

Thanks,
Bahadir

推荐答案

ba ******** **** @gmail.com 写道:


在C中实现重载函数的最佳方法是什么?对于
实例,如果你想为各种
结构使用通用打印功能,我的实现将是一个case语句:

void print_structs(void * struct_argument,enum struct_type stype)
{
switch(stype){
case STRUCT_TYPE_1:
struct type1 * p =(struct type1 *)struct_argument;
printf(" type1.field1:% d\ n,type1.field1);

案例STRUCT_TYPE_2:
...
}
返回;
}
<有一件事我不喜欢这个,你必须在枚举中编码类型
信息。我不喜欢这个,因为我不喜欢将用户定义的方面编码到代码中。有没有更好的方法来做这个?

谢谢,
Bahadir
Hi,

What''s the best way to implement an overloaded function in C? For
instance if you want to have generic print function for various
structures, my implementation would be with a case statement:

void print_structs(void * struct_argument, enum struct_type stype)
{
switch(stype) {
case STRUCT_TYPE_1:
struct type1 * p = (struct type1 *) struct_argument;
printf("type1.field1: %d\n", type1.field1);

case STRUCT_TYPE_2:
...
}
return;
}

One thing I don''t like about this is that, you must encode type
information
in an enum. I don''t like this because I don''t prefer encoding
user-defined aspects into code in general. Is there a better way of
doing this?

Thanks,
Bahadir



lcc-win32是一个实现重载的C编译器功能。
http://www.cs.virginia.edu / ~lcc-win32


请:


flames> / dev / null


lcc-win32 is a C compiler that implements overloaded functions.
http://www.cs.virginia.edu/~lcc-win32

Please:

flames >/dev/null


jacob navia< ja *** @ jacob.remcomp.fr>写道:
jacob navia <ja***@jacob.remcomp.fr> wrote:
lcc-win32是


这里非常偏离主题,你知道这个,该死的你,navia!

请:

flames> / dev / null
lcc-win32 is
Phenomenally off-topic here, and you _know_ this, damn you, navia!
Please:

flames >/dev/null




请:将主题广告发布到您自己的游乐场新闻组。


Richard



Please: off-topic advertisements to your own playground newsgroup.

Richard


jacob navia写道:
jacob navia wrote:

lcc-win32是一个C编译器实现重载的功能。


如果它实现了重载函数,它不是一个C $ / $
编译器。

请:
flames> / dev / null

lcc-win32 is a C compiler that implements overloaded functions.
If it implements overloaded functions, it is not a C
compiler.
Please:

flames >/dev/null




我之前没有反对过你的自我推销;每一个

有用的信号带来一些噪音。但是这篇最新帖子是我看到的最糟糕的一次:这是故意误导的。这不仅仅是广告,而是虚假广告。


我将把未来的火焰发送到/ dev / null如果你发送

你未来的废话也是如此。交易?


-

Eric Sosman
es ***** @ acm-dot-org.inva 盖子


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

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