a member of a pointer class adressed with . instead of ->??

Started by
5 comments, last by Afterlife 22 years, 7 months ago
Yet another of my newbie questions...
    
class Maptile {...};
...
Maptile *world=create_world(xsize+1,ysize+1); //reserves space with new

world[x+y*xsize].terrain=0; //This is the way the compiler accepts it, unlike :

world[x+y*xsize]->terrain=0; //But shouldn't it be like this, sinse it's a pointer??

//Or is this only with structs and not with classes?

    
Edited by - Afterlife on August 18, 2001 4:06:56 PM
------------------------------If there be no heaven,may there atleast be a hell.-------------------------------Afterlife-
Advertisement
No. The ''.'' operator is correct because you are indirectly deferencing the pointer ''world'' when using array subscripting.

typedef struct _tagMyStruct
{
int x, y;
} MyStruct, *MyStructPtr;

MyStructPtr p = new MyStruct[200];

p[100].x = 10; // access 101st element of array - access x
p[100].y = 20; // access 101st element of array - access y

Best regards,



Dire Wolf
www.digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
k, thanks. I still wonder though, do you use the -> operand when you pass such values to a function?
------------------------------If there be no heaven,may there atleast be a hell.-------------------------------Afterlife-
It depends on if you are accessing the variable itself or a pointer to it. Array elements are variables themselves. Whereas the name of the array is a pointer. It''s weired first time you hear it, I guess.

So if you do:

int a[10];

then a[#] (where # is a number between 0 and 9) will give you a direct access to that variable i.e. they are variables, not pointers. It''s probably a bad way to say this but can''t find a simpler way.

''a'' on the other hand, on its own, is a pointer i.e. carries the address to the beginning of the array. (so by sticking [] after it, that sort of makes it a variable. Again, bad way to explain it but it sounds simpler that way)

The rule is, use ''->'' if the name before it is a pointer. Use ''.'' if the name before it is a variable itself.
Array names are syntactically the same as pointers for most purposes, however they are not pointers ... I go on about this quite a lot because it bit me once

However, what I really wanted to do was to add to what PugPenguin said, and note that you also use ''.'' when you are passing by reference as well as for variables. References are pointers which are implicitly dereferenced for you and have certain rules on usage. But I guess if you don''t know that you just won''t use them

--


Games, Anime and more at GKWorld

Meet Bunny luv'' and the girls, ready to perform on your desktop just for you (warning: adult content).

Remember that the array operator [] dereferences the pointed-to array:
x[y] = *(x + y)

BTW, x[y] = y[x], therefore x[1] = 1[x](just in case you want to impress your friends or offend your coworkers).

Edit: one of these days I'll pick variables that aren't swallowed as tags.

Edited by - Stoffel on August 20, 2001 1:29:40 PM
quote:Original post by taliesin73
Array names are syntactically the same as pointers for most purposes, however they are not pointers ... I go on about this quite a lot because it bit me once


While we are at it, could someone elaborate on this? While I hate to get too theoretical as so few seem to be interested, I also don''t want lack of knowledge to bite me in the future - I might as well know it.

This topic is closed to new replies.

Advertisement