what does this do in c++?

Started by
10 comments, last by Nitage 18 years, 10 months ago
i'm looking over some sample code, and i see this "->". it seems to be an operator of some sort, but i don't know how it works or what it does. can someone please explain it to me, or post a link to a page that does? thanks. oh, here is the line of code where i found it, should that matter:

if(FAILED(screen.lpddpal->SetEntries(0,0,MAX_COLORS_PALETTE,bitmap.palette)))
   return 0;



alright, two lines lol.
Charles Reed, CEO of CJWR Software LLC
Advertisement
ugh well i just started using that my self this morning lol.

its for accessing functions that are stored on the heap if im not mistaken. I probably am tho.

Instead of

Class.Function()
its
Class->Function()

like a pointer. get it? -> pointer? :)
actualy that is what it has to do with.
a->b is (mostly) equivalent to (*a).b
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
While you got the general idea it isnt used to acccess functions that are on the heap. It is used to dereferance a pointer to a object and access its member.
Quote:Original post by Leviathan3328
While you got the general idea it isnt used to acccess functions that are on the heap. It is used to dereferance a pointer to a object and access its member.


???

class Foo{    void Bar();}int main(){   Foo *fptr = new Foo;   fptr->bar();}


It works both ways. As fruny says, it is basically a shorthand way of writing "(*a).b".
It's used for accessing Member data on the free store.

For example:
// "Normal class"class Dog{public:void woof() { std::cout<<"Woof!";}void walkaway() { std::cout<<"Walks away...";}private:int Age;}Fido;Fido.woof();Fido.walkaway();std::cout<<"The dog is "<<Fido.Age<<" years old.";// Dog created on the free storeDog *Rover=new Dog;Rover->Woof(); // -> is used to access the class member data created on the // free store - in this case a functionRover->walkaway();std::cout<<"The dog is "<<Rover->Age<<" years old.";// Disclaimer:  I know that Dog.Age==0 just now - it's purely for way of example.  // A properly written program :) // would have an accessor function for setting the age,// maybe accessed using the constructor.


See the difference between the "ordinary" class instance Fido and the "other" type Rover on the free store?

Hope to help,

ukdeveloper.
Quote:It's used for accessing Member data on the free store.


First, the free store is in C. In C++, you usually use the heap instead. The only way to get an 'object' into the freestore is by throwing type safety away.
Secondly, the -> operator is used for derefrencing a memory address. It can be on the heap. It doesn't have to be on the heap. It can be anywhere except address 0. On the stack. In ROM. Anywhere.
Deyja - wrong way round - its free store for new/delete, heap for malloc/free. Though most people use them interchangably anyway, so its a silly distinction to get hung up on.

Your right that it can be anywhere a pointer can go though. As has allready been said, a->b is equivalent to (*a).b - if you don't understand that I recomend you read up on pointers. Or simply take it as fact that for any object declared as 'A * b = new A();' you will have to access members with '->', but when declared as 'A b;' you will use '.'. Of course, your not going to get very far with C++ until you can wrap your head arround pointers, but you can't learn everything at once:-)
To clarify:

-> has nothing to do with where an object is stored.

-> is used to access a member function or variable of an object through a pointer to that object.

a->b is usually equivalent to (*a).b.

However, both operator-> and operator* can be overloaded.
incase your wondering it exists because a->b is faster to type and looks nicer than (*a).b [smile]

This topic is closed to new replies.

Advertisement