J
John
I need an dynamic array of pointers to MyObj in a class.
Am I doing this right?
class SomeClass
{
public: SomeClass();
~SomeClass();
MyObj **pMyObj; // pointer to pointer == pointer to array of
pointers
int iNumPointers; // counter
}
SomeClass::SomeClass()
{
iNumPointers = 123;
pMyObj = new MyObj*[iNumPointers]; // create array of iNumPointers
pointers to MyObj
for (int i = 0; i < iNumPointers; i++) pMyObj = new MyObj(); // init each
pointer object
}
SomeClass::~SomeClass()
{
for (int i = 0; i < iNumPointers; i++) delete pMyObj; // destroy each
object first
delete [] pMyObj; // and destroy array of pointers
}
Is there an easier/smarter way to the same thing to avoid destructors? e.g.
using std::vector?
Am I doing this right?
class SomeClass
{
public: SomeClass();
~SomeClass();
MyObj **pMyObj; // pointer to pointer == pointer to array of
pointers
int iNumPointers; // counter
}
SomeClass::SomeClass()
{
iNumPointers = 123;
pMyObj = new MyObj*[iNumPointers]; // create array of iNumPointers
pointers to MyObj
for (int i = 0; i < iNumPointers; i++) pMyObj = new MyObj(); // init each
pointer object
}
SomeClass::~SomeClass()
{
for (int i = 0; i < iNumPointers; i++) delete pMyObj; // destroy each
object first
delete [] pMyObj; // and destroy array of pointers
}
Is there an easier/smarter way to the same thing to avoid destructors? e.g.
using std::vector?