-1

In C++, I have a class CMyObject as follows, where CData is another class name:

Class CMyObject
{
CMyObject(CData& Data): m_Data(Data) {};
virtual ~CMyObject(void);

private:
  const CData& m_Data;
}

When allocate one CMyObject instance, I can do as follows:

P = new CMyObject(MyData);

However, if I want to create an array of CMyObject, then can I do as follows?

P = new CMyObject(MyData)[10];
alancc
  • 487
  • 2
  • 24
  • 68

1 Answers1

1

You can use an initialization list (I used 4 to save some typing, but you get the idea):

CMyObject* P = new CMyObject[4]{MyData, MyData, MyData, MyData};

But better still, use an std::vector:

std::vector<CMyObject> P(10, MyData);
juanchopanza
  • 223,364
  • 34
  • 402
  • 480