我正在搜索SVM库并遇到BudgetedSVM.
在源代码中,我发现了一个不寻常的用法,就像这样:
#sample.h
#ifndef SAMPLE_H
#define SAMPLE_H
//no header included or namespace declared here
#ifdef __cplusplus
extern "C" {
#endif
//no header included or namespace declared too
class Sample: public Parent
{
public:
Sample();
~Sample();
type0 fun(type1 val1, type2 val2);
...
};
#ifdef __cplusplus
}
#endif
#endif // SAMPLE_H
Run Code Online (Sandbox Code Playgroud)
如图所示,标头中不需要额外的头或命名空间,这些都在cpp文件中.
这是我的想法:
为什么extern "C"(通常用于C接口)将C++类分组?这种用法对某些东西有用吗?
即使type0,type1并type2出现了,自己的头在这里没有包括在内,但在cpp文件(如sample.h).当我调用类的Sample,但是,我必须包括这些报头(例如type0.h,type1.h,type2.h),这似乎很不方便.