生成对象时宏和模板的组合
Combination of macros and template in generating Object
我创建了一个模板类来填充类T的一些对象,但是类T的对象在网络中是共享的,我不想给出关于它们的信息,所以我为每个类T和宏创建一个助手类:
class Entity {
// just Properties
}
class EntityHelper{
// needed method for fill from database
}
#define DBH(x) x##Helper
这是上下文类的函数,用于填充对象的列表
template<class T>
QList<T> ContextClass::query(const QString& q){
T inst;
DBH(T) helper;
// and another methods
}
我收到帮助程序未声明的标识符错误!!如果我不使用函数m->query(q);我没有犯错
我知道我可以用另一种方法来做这件事,但用这种方法怎么了?
更新:
好吧,看来我必须用另一种方法,我用过这个?
template <class T>
class Helper {
Helper<T>* createInstance();
//some methods
}
class EntityHelper : public Helper<Entity>
{
EntityHelper* createInstance();
// query needed for entity table database
}
记住宏只是文本替换,所以您的代码变成:
template<class T>
QList<T> ContextClass::query(const QString& q){
T inst;
THelper helper;
// and another methods
}
这是无稽之谈。。。
在您的情况下,只需使用模板:
template<class T>
class Helper;
template<>
class Helper<Entity>
{
// needed method for fill from database
};
template<class T>
QList<T> ContextClass::query(const QString& q){
T inst;
Helper<T> helper;
// and another methods
}
相关文章:
- C++中的对象组合
- C++对象文件和程序文件组合编译的 Bashrc 别名
- clang libc :make_tuple与make_shared的组合导致早期对象破坏
- C++组合从 istream 获取的类对象
- 覆盖组合对象的<<运算符会给出错误 C++
- 使用 Arduino 中的构造函数进行对象组合
- Qt C++根据组合框中的选择创建一个对象
- 使用参数组合创建对象
- 面向对象编程和对象组合,如何放置组合属性?
- 如何在组合中在运行时更改成员对象?
- OpenCV中的矩阵乘法 - 组合CV :: MAT和CV ::标量对象
- 在列表中对对象进行排序并组合值
- 将具有默认值的构造函数定义为组合对象的私有字段
- C++ 组合,对象如何知道它所在的类
- c++ 在对象组合上使用重载构造函数
- 对象组合与提供显式包装器
- 在C++中定义许多组合对象的最佳方法
- 在c++中,当组合对象构造函数有依赖关系时,如何在初始化列表中强制它们的顺序
- C/C++.库相对于组合对象文件的优势
- 如何在使用 gcc(不是 g++)编译的 C 项目中组合C++对象文件