Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 02-22-2004, 10:11 PM   #1 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
. vs ->

this is very noob i'm sure.

i've been disecting some c++ code to get a better understanding of it.

i was looking a the code for a half-life mod and i see the use of both -> and . after an object.

i'm familiar with using -> , but what is the '.' and when do you ue it?

i.e.

object->property
object.property
__________________
Mike
sde is offline   Reply With Quote
Old 02-22-2004, 11:34 PM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,693
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
Old 02-22-2004, 11:34 PM   #3 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
the . operator is used to to access a member of a local struct or class instance. the -> operator is for accessing members of a pointer to a struct or class instance.

essentially, s->m is equal to (*s).m
and s.m is equal to (&s)->m
joe_bruin is offline   Reply With Quote
Old 02-23-2004, 01:21 AM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
thanks a lot, that makes sense now.
__________________
Mike
sde is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
PiNg FlOoDiNg bluedevils1414 Lounge 21 08-13-2003 08:21 PM


All times are GMT -8. The time now is 03:56 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting