• Advertisement

Archived

This topic is now archived and is closed to further replies.

How do I create a new array of pointers to an object? syntax...

This topic is 5493 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. I am trying to allocate an array of pointers to an object. I am not quite sure of the syntax and the compiler is complaining. Basically I want this: CObject* pObject = new CObject*[n]; so that pObject will be a pointer to an array of CObject pointers (objects will be created later). The compiler doesnt like the above, it says something about not being able to case CObject** to CObject*.

Share this post


Link to post
Share on other sites
Advertisement
Array of pointers. Normally, if you were allocating an array, you would use a pointer, correct? So then an array of pointers is a pointer to pointers, so you need a **.

CObject** pObject = new CObject*[n];

Note the double star on the left side.

tj963

Share this post


Link to post
Share on other sites
A dynamic array to objects of type T is represented by a pointer of type T*. Similarly, a dynamic array of pointers to objects of type T is represented by a pointer-to-pointer of type T**. Hence,
CObject** pObject = new CObject*[n]; 

Share this post


Link to post
Share on other sites
If I understand u right, You need to use CObject** pObject = new CObject*[n];
which is a pointer to an array of pointers to CObjects rather than CObject* which is a pointer to an array of objects;

Share this post


Link to post
Share on other sites
Thanks! I will try that. The clues were there, my brain is just fried after a humdinger of a bug I had yesterday. :o(

Share this post


Link to post
Share on other sites

  • Advertisement