C语言 C 函数可以有多个签名吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7614672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:46:13  来源:igfitidea点击:

Can a C function have multiple signatures?

cfunction

提问by RLH

Possible Duplicate:
function overloading in C

可能的重复:
C 中的函数重载

Apologies if this is a dup but if it is, I can't find it.

抱歉,如果这是重复的,但如果是,我找不到它。

In C, can you define multiple functions with the same function name, but with different parameters? I come from a C# background. In C#, the following code is completely legal.

在 C 中,您可以定义多个函数名称相同但参数不同的函数吗?我来自 C# 背景。在 C# 中,以下代码是完全合法的。

//Our first function

//我们的第一个函数

int MyFunction()
{
    //Code here
    return i;
}

int MyFunction(int passAParameter)
{
    // Code using passAParameter
    return i;
}

In my specific case, I would like to create a function that has one optional parameter (that is an int) at the end of the parameter list. Can this be done?

在我的特定情况下,我想创建一个函数,该函数在参数列表的末尾有一个可选参数(即 int)。这能做到吗?

回答by James McNellis

No. C does not support overloading.

不,C 不支持重载。

回答by abelenky

No. In strict C, you cannot do overloading.

不。在严格的 C 中,您不能进行重载

However, given that most C compilers also support C++, and C++ does support overloading, there's a good chance you can do overloading if you're using a mainstream C/C++ compiler.

但是,鉴于大多数 C 编译器也支持 C++,并且 C++ 确实支持重载,如果您使用的是主流 C/C++ 编译器,则很有可能进行重载。

But its not strictly standard or portable to pure-C environments.

但它不是严格的标准或可移植到纯 C 环境。

回答by Foo Bah

No you must use a different name for each function (this is not true with C++, as it allows you to specify optional parameters)

不,您必须为每个函数使用不同的名称(C++ 不是这样,因为它允许您指定可选参数)