C++ question (excuse me)

Started by
1 comment, last by penetrator 22 years, 8 months ago
i''ve seen in some codes the use of the following instruction or operator -> and i don''t know what it is ... glHorizon_Project

www.web-discovery.net

Advertisement

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

This topic is closed to new replies.

Advertisement