[英][MAKEFILE]: How to copy cpp source files from different source folders into one destination folder and use those cpp files to build using MAKE
[英]Create multiple object files from one source file using CPP flags
我目前有一个用C ++定义的类,其中包含一个C-preprocessor变量来打开/关闭某些功能:
A.hh
class A : public B {
//...
#ifdef PPVAR
int _i;
#endif
public:
A();
//...
};
A.cc
A::A()
#ifdef PPVAR
: B(1)
#else
: B(2)
#endif
{}
这会影响成员变量和超类的初始化。 目前,我通过在c ++编译器标志中包含/排除“ -DPPVAR”来在两种情况之间切换。 然而,使两个实现同时可用将是更可取的。 从原则上讲,我可以复制这些源文件(A_on。[hh,cc],A_off。[hh,cc]),但是由于它们有很多共同点,因此看起来很不雅致。 因为该标志影响超类的成员和构造函数,所以我看不到通过简单地引入全局变量bool switchFeature = [T|F]
并使用以下简单方法来控制此功能的简单方法
if (switchFeature) {
//...
} else {
//...
}
everywwhere。 如果可能的话,这将是我的选择方法,我很想听听您对此的解决方案!
作为一种替代方法(尽管将类的数量增加了一倍),我认为也许可以编译一次此源,一次使用“ -DPPVAR”,一次不使用并生成两个目标文件A_on.o,A_off.o,但是无论如何我仍然需要复制我的标头以使其正常工作,并且需要在其余代码中进行更多修改。
我很感激我如何解决这个问题的任何见解和提示
使您的班级成为int
A
模板。
template<int PPVAR_val = 1>
class A : public B {
//number of members can also differ for partial specializations
int _i;
public:
A();
//...
};
A.cpp
template<int PPVAR_val>
A::A()
: B(PPVAR_val)
{}
如果A和它的子代之间的某些代码不同,则应为A创建一个子代并添加虚拟方法
A.hh
class A : public class B {
// Common attributes and Functions
...
protected:
A(int ppvar) : B(ppvar) {}; // Not public
public:
A() : B(1) {};
virtual method(); // Method that differ between A and its Child
...
};
class A2 : public class A {
// Attributes only needed by A2
...
public:
A2() : A(2) {};
method(); // Say that A2 will use its own implementation of method
};
main.cc
int main(void)
{
bool PPVar = ...;
A *a;
if (PPVar)
a = new A();
else
a = new A2();
a->method();
return (0);
}
具有两个子AOn和Aoff的抽象类A。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.