【问题标题】:c++ How do I call a constructor with arguments of a class that is inside an array of structures when I create the array of structures?c++ 当我创建结构数组时,如何使用结构数组内的类的参数调用构造函数?
【发布时间】:2019-02-02 23:45:13
【问题描述】:

如何将构造函数参数传递给结构数组中的类? 我正在尝试使用链表实现。 我有一个区域数组,每个区域都有一个linkedList,Sll。 (实际上我会有一个二维数组。)我无法找到/弄清楚使用非默认参数/参数实际调用构造函数的语法。

我想将 Sll 保留为一个类。

class Sll
{
  public:
  unsigned long count;
  unsigned long uMaxEntries;
  void *pDataArray;
  int iSizeOfEachEntry;
  Sll( unsigned long uInSizeOfEachEntry,  unsigned long uInitNumEntries = INIT_NUM_ENTRIES );
}

Sll::Sll(unsigned long uInSizeOfEachEntry, unsigned long uInitNumEntries)
{
  uMaxEntries = uInitNumEntries;
  iSizeOfEachEntry = uInSizeOfEachEntry;
  pDataArray = malloc(iSizeOfEachEntry * uInitNumEntries);
  count=0;
}

...
struct Region {
  int lotsOfOtherStuffToo;
  Sll sllParticles;
};
...
  for(int ii = 0; ii < regionsX; ++ii)
      ppRegions[ii] = new Region[regionsY];  // How call the Sll constructor?

【问题讨论】:

  • 新位置..

标签: c++ class constructor structure new-operator


【解决方案1】:

这里的数组有点让人分心。

您有一个不能默认构造的类Region,因为它包含一个也不能默认构造的Sll 类型的成员。所以,你给它一个构造函数。

然后,解决了这个问题,您回到数组:如何创建非默认 Region 对象的数组? By using a vector.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-17
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-03
    相关资源
    最近更新 更多