Jump to content
  • Advertisement
Sign in to follow this  
CJWR

what does this do in c++?

This topic is 4810 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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".

Share this post


Link to post
Share on other sites
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 store

Dog *Rover=new Dog;

Rover->Woof(); // -> is used to access the class member data created on the
// free store - in this case a function

Rover->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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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:-)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
incase your wondering it exists because a->b is faster to type and looks nicer than (*a).b [smile]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!