我有一个项目应用程序,在这个项目中我有两个文件夹“behavior1”和“behaviour2”
Application
|_ behaviour1
| |_ action
| |_ action.h
| |_ action.cpp
|
|_ behaviour2
| |_ action
| |_ action.h
| |_ action.cpp
|
|_ behaviour_type.h
|_ app.cpp
每个目录中的action.h定义抽象类
中的派生类class AbstractAction {
public:
virtual ~AbstractAction() = default;
virtual void Execute() = 0;
}
但实施方式不同。
然后使用文件“behaviour_type.h”中的下一个代码
#define CURRENT_BEHAVIOUR bevahiour1
#define QUOTEME_1(x) #x
#define QUOTEME(x) QUOTEME_1(x)
#define INCLUDE_FILE(x) QUOTEME(CURRENT_BEHAVIOUR/x)
在“app.cpp”中我可以写
#include "behavior_type.h"
#include INCLUDE_FILE(hook/action.h)
...
AbstractAction* action = new Action();
action->Execute();
但在这种情况下,将编译两个“action.h”(来自behavior1和behavior2)。我想只编译一个,我包括在内。有可能吗? 仅使用源代码(即我的INCLUDE_FILE指令)编译一个“action.h”文件,而不是编译器属性。
可能这个问题很傻,请原谅,但我是新手。 感谢。
答案 0 :(得分:1)
我不知道你的宏做了什么或应该做什么,但为什么不使用简单的变体:
#ifdef ACTION_1
#include "behaviour1/hook/action.h"
#else
#include "behaviour2/hook/action.h"
#endif
AbstractAction* action = new Action();
action->Execute();
那就是说,我完全支持那些说你应该同时使用某种模式而不是条件编译的评论。