Advertisement

Help, how do I do an array of pointers in C++

Started by June 04, 2000 04:48 PM
6 comments, last by Dr_Evil 24 years, 6 months ago
tObject **listOfObjects = (tObject **) new tObject[4]; This should create me a list of pointers to tObjects shouldnt it? Problem is, tObject is an abstract class. When I compile, I get the error "Cannot instantiate abstract class due to the following : tObject::~tObject() tObject::getDiffuse() etc. " Now it looks like Im actually creating an array of objects, as we shouldnt have a problem instantiating when only using pointers. So, the question is : Am I creating the object? If so, what is the correct syntax for this?
(tObject **) new tObject[4];

doesn''t create an array of pointers to tObject, it creates a array of tObject and then forces the pointer (array) to a pointer to tObject.

Try using this instead:
new (tObject*)[4];

(I haven''t tested that code)
Advertisement
tObject **listOfObjects = new *tObject;for (int i = 0; i < numPointersWanted; i++){    listOfObjects = new tObject;}[\code]     
goir: new (tObject*)[4]; doesnt compile ("missing '';'' before '']'')

new (tObject *[4]) does though. and that runs, but so did (tObject **) new tObject[4], so it doesnt mean its necessarily right.

ncsu: tObject **listOfObjects = new *tObject;

Doesnt that just point to an array of objects rather than an array of pointers to objects, which is what Im after?
Of course, now Ive got to delete them too!

I dont want to delete the actual contents of the pointers, so how about:

delete (tVector**) listOfObjects;

Edited by - Dr_Evil on June 4, 2000 6:10:06 PM
no, that does point to an array of pointers


to delete the way i have constructed it,
just call
delete [] listOfObjects;     


you are quasi-evil, simi-evil, the diet coke of evil
just one calorie, not evil enough


Edited by - ncsu121978 on June 4, 2000 6:13:18 PM
Advertisement
That''s correct Dr_Evil about it not necessarily beeing right. What you do is tell the compiler that it really points to **tObject. For what the compiler cares you can say a pointer to int points to double, it compiles but in most cases you don''t get a good result.

My code has a fault (I think). It should be:
new (*tObject)[4];
ncsu :

tObject **listOfObjects = new *tObject; // How can I assign the number of pointers that I need here?
for (int i = 0; i < numPointersWanted; i++)
{
listOfObjects = new tObject; // Presume you mean listOfObject = new tObject. Wont this mean we''re indexing uninitialised memory?
}

= new tObject // create a new object
= new *tObject// create a new pointer to an object, which could also be used as an array of objects.

Ive lost track completely, but reguardless, it doesnt compile so I''ll stop there.

BTW, I didnt spend 3 years at Evil school to be called The diet coke of evil!

This topic is closed to new replies.

Advertisement