我試圖初始化模板類中的數組,並將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爲您的答案,很好,很容易 –