Thread: . vs ->
View Single Post
Old 02-22-2004, 11:34 PM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
It's just a way of derefferencing the needed instances within a class, ie:

Now this is strict C, but classes/structs they're almost the same spare me your explaining that structs can't be compared with classes...
Code:
struct width_struct{
  int width;
};

struct count_struct{
  unsigned long int count;
  struct width_struct width;
  struct count_struct* next;
  struct count_struct* prev;
};

int width_count = 0;

int some_function(struct count_struct* counter)
{
  static int count = 0;
  if(width_count == 1000)
    return -1;
  counter->width.width = width_count %50;
  counter->count = count++;
  width_count = ((width_count + 28)*3)%1000;
  return 0;
}


int main ()
{
   int counter = 0;
   static int my_count = 10;
   struct count_struct* cur_struct;
   struct count_struct* next_struct;
   cur_struct = (struct count_struct*) malloc(sizeof(struct count_struct));
   cur_struct->next = NULL;
   cur_struct->prev = NULL;
   while(counter++ <= my_count)
   {
     if(0 == some_function(cur_struct))
     {
       next_struct = (struct count_struct*) malloc(sizeof(struct count_struct));
       cur_struct->next = next_struct;
       next_struct->prev = cur_struct;
       next_struct->next = NULL;
       cur_struct = cur_struct->next;
     }
     else
       return -1;
   }
   do
   {
     printf("count_struct %d\n\tWidth: %d\n\n", cur_struct->counter,
                                            cur_struct->width.width);
     cur_struct = cur_struct->prev;
   }
   while(cur_struct != NULL)
   return 0;
}
depending on what current level in your struct you're refering to, you will either use '->' or '.' aint poniters fun working with.

Same thing with your object.property vs. object->property if your current level of passing by reference requires the '.' for accessing the class variables, then thats what is beeing used, if at that point it's the '->' which is required, then a good programmer would often tend to use it.

Note: The above code example was written without any testing what so ever, it might fail and currupt your data or something
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote