使用"this"指针初始化 std::array
initialize std::array with 'this' pointer
我试图在模板类中初始化数组,并将this
指针传递给数组中的所有元素。这就是我的班级的样子:
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中)?
首先,在构造函数或任何其他成员函数之外,您不能像这样的this
。在这里,您必须在Initializer列表中初始化cl
。
将委托构造函数与std一起使用::*_序列内容:
template<int NUM> class outer_class {
...
template <std::size_t... Integers>
outer_class(std::index_sequence<Integers...>)
: cl{(static_cast<void>(Integers), this)...}
{}
public:
outer_class(/* whatever */) : outer_class(std::make_index_sequence<NUM>{}) {}
};
旁注:
- 您的
print
成员功能应标记为const
,因为它们不修改您的会员。 -
cl[NUM - 1].print_num();
您可能要使用std::array::back()
。
您可以使用一些辅助功能,然后使用这些功能初始化成员,例如:
template <std::size_t I, class T>
T copy(T t) { return t; }
template <class T, std::size_t... Is>
constexpr std::array<T, sizeof...(Is)> copy_n(T const& t, std::index_sequence<Is...>) {
return {copy<Is>(t)... };
}
template <class T, std::size_t N>
constexpr std::array<T, N> copy_n(T const& t) {
return copy_n(t, std::make_index_sequence<N>{});
}
然后在您的课程中:
std::array<inner_class<NUM>, NUM> cl;
outer_class() : cl(copy_n<inner_class<NUM>, NUM>(this)) { }
注意:
- [要验证] 您不能在默认成员初始化器中使用
this
,因此您需要具有自定义构造函数; - 您需要将
inner_class<NUM>
明确指定为copy_n
的第一个模板参数,因为将其他T
推断为outer_class<NUM>*
,并且虽然从outer_class<NUM>*
到inner_class<NUM>
的隐含转换,但从std::array<outer_class<NUM*>, NUM>
转换为CC_18; li>如果您使用的是C 11,而不是14或
clang
,则可能会在copy_n
的return
上发出警告,您可以通过添加一对额外的支架{}
来摆脱它。
相关文章:
- C++11 中不同类型的对象的 std::array 的替代方案
- constexpr begin of a std::array
- C++如果必须在编译时确定大小,std::array 有什么意义?
- 标准::unordered_map 中的 std::array 的值初始化
- 确保编译时的特定 std::array 位置
- std::array的长度有大小限制吗?
- 将 std::array 移动到另一个 std::array
- 检查输入 std::array 指针数据是否等于某个常量数组
- 如何读/写或遍历 std::array 中的特定元素范围?
- 是否可以使用 std::array 作为 POD 结构的数据容器?
- 如何在C++中传递一个大小未知的 std::array?
- 将 std::array 推回 std::vector N 次的优雅方式
- 如何在 c++11 中静态断言 std::array 类成员进行排序?
- unqualified sort() -- 为什么它在 std::vector 上使用而不是在 std::array 上
- 按值返回 std::array
- 如何将变量内容常量转换为 std::array 的大小?
- 将初始化器列表/聚合初始化转发到 std::array 成员
- 无法推断类中的类型,构造函数采用 std::array
- 为什么 char 可以在 std::array 中初始化为 nullptr,而不是单独初始化?
- C++ IDE 不会推断/自动完成对模板类中的 std::array 下标表达式的成员访问