【发布时间】:2018-11-07 00:43:15
【问题描述】:
从Here看来,C 不支持默认参数。
我在导出库中有以下方法:
extern "C"
{
__declspec (dllexport) uintptr_t Method(int freq, int *pRetval, bool *support2MHz);
}
如果我像这样将最后一个参数设为可选:
extern "C"
{
__declspec (dllexport) uintptr_t Method(int freq, int *pRetval, bool *support2MHz = NULL);
}
我的 dll 仍在编译。 我的问题是为什么?每个人都说 C 代码不支持默认参数。
我在 MS 2015 中使用 C++。
【问题讨论】:
-
我认为它有效,因为默认参数由调用者处理
-
extern "C"并不表示“这是 C 代码”,它只影响导出符号的形式(即“方法”)。如果你将它提供给 C 编译器,它会同时抱怨extern "C"和默认参数。 -
据我所知,当您尝试使用此导出时,您仍然需要提供 3 个参数。这是c++,所以你可以在这里提供默认参数,但是当你尝试使用导出的C函数时,你需要提供所有3个。
-
@Afshin "使用导出的 C 函数",你的意思是使用导出的 C 代码还是 C++ 代码?
-
@John 我的意思是当你想在 C 代码中使用导出的 C++ 代码时。