c – extern C不能在班级使用?

只想在Windows环境下确认VSTS 2008 C项目,我们只能将extern C应用到功能级别,而不能应用于类级别(这样所有成员函数都从类中使用C语言名称修改)?我尝试了几种方法,但始终编译错误.

提前致谢,
乔治

解决方法:

您可以通过非常复杂(但完全合法)的黑客将extern“C”应用于成员函数:

extern "C" typedef int bar_t(int x);

struct foo {
     bar_t bar; // yes, this declares a nonstatic member function!
};

int foo::bar(int x) { return x; } // definition

这可以根据ISO C 03 9.3 [class.mfct] / 9:

a member function can be declared (but not defined) using a typedef for a function type. The resulting member function has exactly the same type as it would have if the function declarator were provided explicitly, see 8.3.5.

但是,由于ISO C 03 7.5 [dcl.link] / 4,这并不能给你带来任何好处.

A C language linkage is ignored for the names of class members and the member function
type of class member functions.

上一篇:c – 默认情况下全局变量是extern还是等同于在全局中使用extern声明变量?


下一篇:Linux中Tomcat 自动设置CATALINA_HOME方法