可以在create()
函数中自动镜像/重复test
构造函数获取的参数,并将它们传递给构造函数,而不使用预处理器宏?
注意1:create()
函数必须是extern“C”
,以避免名称混乱-它将是一个。so库,通过dlopen()
/dlsym()
在另一个C++程序中使用。 我试过模板,但似乎extern“c”
函数不接受它。
注2:首选的解决方案将是运行时开销为零的解决方案(如果可能的话)
这个想法是:
class Test {
public:
Test(/* some args */) {
}
/* other methods */
};
extern "C" Test* create(/* same args of Test class */) {
return new Test(/* pass same args of this function */);
}
为了避免重复,您可以创建一个参数结构:
struct TestParams
{
int x, y, z;
};
class Test {
public:
Test(TestParams params) {
}
/* other methods */
};
extern "C" Test* create(TestParams params) {
return new Test(params);
}
或者你可以硬着头皮复制粘贴参数。 最后,别忘了代码只写一次,但要读多次。