我试图初始化模板类中的数组,并将this
指针传递给数组中的所有元素。 这是我的课是什么样子:使用'this'指针初始化std :: array
template<int NUM> class outer_class;
template<int N>
class inner_class {
private:
outer_class<N> *cl;
public:
inner_class(outer_class<N> *num) {
cl = num;
}
void print_num() {
cl->print_num();
}
};
template<int NUM> class outer_class {
private:
int number = NUM;
// --> here I basically want NUM times 'this' <--
std::array<inner_class<NUM>, NUM> cl = { this, this, this, this };
public:
void print_num() {
std::cout << number << std::endl;
}
void print() {
cl[NUM - 1].print_num();
}
};
int main() {
outer_class<4> t;
t.print();
return 0;
}
我怎样才能通过this
指针存储的outer_class
阵列中的所有inner_class
元素(在C++ 11)?
如果序列构造函数是私有的,会不会更好? – Rakete1111
@ Rakete1111对,编辑。 –
@奥尼尔Thx为您的答案,很好,很容易 –