【问题标题】:how to properly instantiate an array of pointers in constructor in c++?如何在 C++ 的构造函数中正确实例化一个指针数组?
【发布时间】:2014-02-25 20:24:36
【问题描述】:

关于通过构造函数实例化数组的简单问题。 我有一个指向类 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)
{

}

【问题讨论】:

    标签: c++ arrays constructor instantiation


    【解决方案1】:

    使用值初始化:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-23
      • 2018-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多