Advertisement

what is this "->" for

Started by May 08, 2002 04:56 PM
6 comments, last by gamefr 22 years, 7 months ago
I see this simble in code and I don''t know what it means, does it have to do with pointers?
"Only to be a humble programmer is a goal I must achieve..."
It has everything to do with pointers...

struct Test_S
{
float member;
};

Test_S Test;
Test_S *Test_ptr = Test;

Test.member = 10.0f;

Test_ptr->member = 10.0f;

As you can see.. with the non-pointer, you use the . and with the pointer, you have to use the -> I don''t know what it technically means, but this is how it''s used. It makes sense, as you''re not accessing a member of the struct, but a member of the struct you''re pointing to, so to show this, you use different syntax, otherwise everyone would forget if it was an actual struct, or just a pointer to one.

Billy - BillyB@mrsnj.com
(Hey, I''''m no longer anonymous!)
Advertisement
yeah it has to do with pointer. When you get around to learning pointers the -> symbol will make total sense.

if you want a sneak peak at pointers:
http://www.cplusplus.com/doc/tutorial/tut3-3.html

-me
this is used when a pointer of a class (Dog *Hunter = new Dog calls a class function from main(Hunter -> Bark()). It's sort of like how an object calls functions with the .

EDIT: stupid smiley wink thing is appearing and I can't make it go away.
[edited by - viper35al on May 8, 2002 6:07:03 PM]

[edited by - viper35al on May 8, 2002 6:07:53 PM]

Test_S Test;
Test_S *Test_ptr = Test;


''->'' saves you having to do this :

(*Test_ptr).member = 10.0f;

SO you can just do :

Test_ptr->member = 10.0f;

Much nicer

If you not confused, your not doing it right!
|< If your not confused, your not doing it right! >|
Thx for the replies i understand completely, I have learned pointers before to some extent, and now I know what that means.

Thx again.
"Only to be a humble programmer is a goal I must achieve..."
Advertisement
Simly put, it is an alias for an object to one of the object''s memebers.

Ex:

pointer1->test();

You could also do this:

object1.test();

(Providing that pointer one is pointing to object one)

You use -> for a pointer and . for an object.


K I L A H M A N J A R O !

Wachar's Eternity <-<-<-<-<- Me own site!
The reason it's like this is because the "."(dot) operator has higher precedence than the multi purpose "*"(multiplication/indirection etc...) operator. So the object isn't being "dereferenced" properly.

If you have a book on C/C++ it probably has a section on operators and more than likely it has an "Order of Operation" table or "Operator Precedence" Table.

Basically it's just like the operators in Algebra only with a few more added in.

BTW the "->" Operator is called the "member-access" operator

struct Geek {
    void e_mail = belgedin@earthlink.net;
    void url;
    int age = 17;
};

[edited by - Xanth on May 9, 2002 2:53:35 AM]
"I thought Genius lived in bottles..." - Patrick Star

This topic is closed to new replies.

Advertisement