Skip to main content
gamedev.net
🔒 Locked

pointers to pointers or something like that...

Started by TheProx 4 replies 300+ views
Original Post
TheProx
TheProx
I am using Place *Grid[101] = new Place; to hold a "part" of a grid for a habitat simulation I am making as a summer project. It will not recognize it as a pointer to a class...I dunno, I know I have done this before... The error is error C2440: ''initializing'' : cannot convert from ''class Place *'' to ''class Place *[101]'' Does anyone know how to do this the "right" way?
NARF - Pinky and the Brain
llvllatrix
llvllatrix
If i read it corrctly your trying to make a pointer to a pointer to a place a new place. But i think the error says different

Here''s my interpretation:
Basically you''ve created an array of pointers. The reason your trying to make a pointer to a pointer to a place a new place is because you are attempting to make the pointer of the array a new place (hope im making sense).

You could just do Grid[101] and it will do what i think you want it to do (make an array of places). Or if Grid has to be dynamic:

Place * Grid;
//101 can be x (ie read from a file or something)

Grid = new Place[101];

Or if you want an array of pointers to places then you''re going to have to do this:

Place * * Grid;
//the reason im not doing * Grid[101] is so that the code is scalable - 101 can be x

Grid = new * Place[101];

for(int x = 0; x < 101; x++)
{
Grid[x] = new Place;
}
TheProx
TheProx
I GOT it! Thanks a lot!
NARF - Pinky and the Brain
TheProx
TheProx
new problem for all you problem solvers.
whenever I make this call:

if(Grid[Loc].GetPrimary == 1)

I get this error:

error C2446: ''=='' : no conversion from ''const int'' to ''int (__thiscall Place::*)(void)''

Why doesn''t this work?? and how do I fix it?
NARF - Pinky and the Brain
Krohm
Krohm
When working with pointer-to-pointers is quite easy to mess up with the following concepts:
1- The pointer to the array of pointers to elements
2- The object being pointed by a pointer in the array
3- The pointer in each array element.

In pure pointer syntax
"Grid[Loc].GetPrimary() == 1" means "(Grid + Loc)->GetPrimary() == 1"
But "(Grid + Loc)" is a pointer to a pointer in your array and the '->' operator applies to pointer to objects and not to pointer to pointers. Do you get the difference?
You want to call the GetPrimary() func on the object being pointed by the pointer in the "Loc" position in the array, which is uh... pointed.

To do this, you obviously need to extract the "value" of the pointer contained in the location, which is a pointer itself.
This becomes...
"(*(Grid + Loc))->GetPrimary() == 1" (probably it needs less parenthesis than that. In array like mode, (never tried) should look like "*(Grid[Loc])->GetPrimary() == 1".

Do you get how it works?
This always works in my programs.

Similar considerations applies to public member data fields.

EDIT: small additions into 1-2-3 part.

[edited by - Krohm on June 2, 2003 3:48:07 AM]
Previously "Krohm"

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.