【发布时间】:2018-04-23 09:26:21
【问题描述】:
我有一个名为 system 的类。系统采用一些对象管理器并以某种方式更改其中的所有对象。
例如,可能有一个系统在 imageManager 中绘制所有图像。
每个派生类的工作方式有点像这样(伪代码):
class someChildClass : public System{
private:
someObjectManager &mang1; //these are used by the update method.
someOtherObjectManager &mang2;//the update method changes these somehow
public:
someChildClass(someObjectManager &mang1, someObjectManager &mang2)
:mang1(mang1),mang2(mang2){
}
virtual void update(){
//this is pure virtual in the System base class.
//Do something with the managers here
}
}
我想写所有东西,但更新方法是浪费时间和错误的来源。我想编写一个宏,基本上可以像这样创建一个这样的类:
QUICKSYSTEM(thisIsTheSystemName, someObjectManager, mang1, someOtherObjectManager, mang2, ... (infinite possible Managers. So a variadic macro?)){
//this is the update function
}
}//this is the end braked for the class declaration. Its ugly but I dont know how I could do the function differently?
好吧,我在制作宏时遇到了一些问题。一切正常,直到我需要将可变参数拆分为名称和类型。我现在不知道这是否可能,因为我不能轻松地在参数中来回切换或对它们应用简单的步骤以确保每个 2nd 都是变量的名称。我可以忽略名称的可能性,只是让类型具有某种自动命名(manager1、manager2、manager3 或类似的东西)。
如果使用宏无法做到这一点,那么在构造函数和类声明部分避免错误并缩短一些时间的更好方法是什么?
【问题讨论】:
标签: c++ variadic variadic-macros