【发布时间】:2018-08-01 09:54:44
【问题描述】:
我不是专业的 C++ 开发人员, 我不确定是否可以做我想做的事。
我有一个模板类,例如:
template < typename T >
class table
{
public
table ( AnotherClassPtr* handler, int s);
table<T>& operator<<(const T& table);
table<T>& operator>>( T& table);
...
}
我有一个标准课程。在这个类中,我想创建一些 table 对象的成员。 es:
class A{
public:
...
AnotherClassPtr* pointer;
table<aCertainStruct> myTable;
...
}
它不起作用,因为我错过了table 的默认构造函数。但是我无法创建默认构造函数,因为我需要将指向 AnotherClassPtr 的指针传递给 table 类,该指针是在我想调用为 @987654327 定义的构造函数之前在 A 类的构造函数中创建的@。 es:
//inside the constructor of the A class.
....
pointer= getAnotherClassPtr();
table<aCertainStruct> myTable ( pointer, 42);
...
[尝试] 下面的东西可以编译,但它不像我预期的那样工作。事实上,如果我尝试调用类表的某些函数,它就不起作用。
class A{
public:
...
AnotherClassPtr* pointer;
table<aCertainStruct> myTable ( AnotherClassPtr*, unsigned int)
...
}
// in the constructor of the A class
....
pointer= getAnotherClassPtr();
table<aCertainStruct> myTable ( pointer, 42);
...
正如我所说,这不起作用;
问:是否可以将模板类变量创建为非模板类的成员?
或者在其他工作中:是否可以在A 类声明中定义一个table 成员,然后在A 构造函数中定义调用我的用户定义构造函数的表对象?
对不起,我的英语不好,不是我的母语,我希望问题很清楚。
提前致谢。
【问题讨论】:
标签: c++ class templates constructor