如何在 C++ 中的构造函数中正确实例化指针数组

how to properly instantiate an array of pointers in constructor in c++?

本文关键字:实例化 指针 数组 C++ 构造函数      更新时间:2023-10-16

关于通过构造函数实例化数组的简单问题。我有一个指向类 x 的指针数组,我正在尝试通过构造函数将数组成员设置为 nullptr。

这是我的 y.h

#include <array>
#include "x.h"
class y
{
public:
static const size_t number = 20;
y();

private:
std::array<x*, number> arrayList;
};

这是我的Y.cpp

#include "y.h"
#include "x.h"
#include <array>
using namespace std;
y::y()
: arrayList(nullptr)
{
}

使用值初始化:

y::y()
: arrayList()
{
}

y::y()
: arrayList{}
{
}