• Advertisement

Archived

This topic is now archived and is closed to further replies.

C++ question (excuse me)

This topic is 6057 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

Advertisement
Guest Anonymous Poster

Does it show up like this?

AircraftObject->HorizontalSpeed++;

the "->" works to dereference pointers for classes... so if Aircraft were a class we''d create a pointer to an instance of it

Aircraft* AircraftObject = NULL;
AircraftObject = new Aircraft(whatever);

so you can reference all the member data and methods like this:
(*AircraftObject).HorizontalSpeed++;
or
AircraftObject->HorizontalSpeed++;

They work the same way. All "->" is for is to be a more intuitive way to work with pointers and to save you typing one character. Also makes code a lot easier to read...

Share this post


Link to post
Share on other sites
^ Yeah, what (s)he said. :D

Additionally, you can also think of it in the same way as you think of arrays, because the operator [] is a dereferencing tool just like the operator ->. The [] operator is more versatile (hence why it''s used in arrays -- simple integers are easy to understand) as you can easily move down a chain of similarly-sized things, whereas -> simply points to the first, leaving cycling through memory addresses as the programmer''s responsibility (which is how standard library iterators work, by adding sizeof(THE_DATA_TYPE) to the value of the pointer).

So essentially, something->somethingElse is identical to something[0].somethingElse... Of course, some times it''s better and more readable to use the [] notation, and sometimes the -> notation.

~ Dragonus

Share this post


Link to post
Share on other sites

  • Advertisement