【发布时间】:2014-01-13 19:18:58
【问题描述】:
我有一个模板类A,我会知道T 取决于哪个类调用它。
例如,有 10 个类将使用类 A,10 个类中的一个称为file1。
我可以编写file1类中所示的代码吗?
class D;
template<typename T>
class A
{
protected:
int a;
int b;
static T *ptr;
public:
static void set_a(int aa){ a = aa; }
static D *create()
{ return new T(); }
};
我可以做如下的事情
class file1
{
#define T file1
A<T>
#undef
.....other data member vs member function
}
class file2
{
#define T file2
A<T>
#undef
.....other data member vs member function
}
原始代码如下所示:
另一个宏1在哪里
#define anotherMacro1(method)\
public:\
static return_type functionname(passingtype *ptr1, passingtype *ptr2)\
{\
return ((T *)ptr1)->method(ptr2);\
}
================================================ ========================
A也是一个宏
喜欢
#define A \
protected:\
int a;\
int b;\
static T *ptr;\
public:\
static void set_a(int aa){ a = aa; }\
static D *create()\
{ return new T(); }
================================================ ================================
class file1_orginal
{
#define T file1_orginal
A()
anotherMacro1(passingValue);
anotherMacro2(passingValue);
#endif
.....other data member vs member function
}
首先我想删除A中的宏,所以我用A类来替换宏A。
【问题讨论】:
-
virtual static函数?哪个类被称为“使用”?#undef没有宏名称?是 C++ 吗? -
'10 个类中的一个被称为“使用”'
using是一个 c++ 关键字!!那特别不会编译... -
为什么不直接将类作为模板参数传递呢?我真的没有得到你想要达到的目标。
-
除了
using是一个 c++ 关键字之外,您是否尝试这样做A<using>? -
这个问题似乎是题外话,因为它是关于缺乏对语言的最低限度的理解。