C++ '-'

Started by
10 comments, last by chollida1 17 years, 9 months ago
Can someone explain to me what the -> is in C++? I'm more fluent in Java, so if possible is it comparable to anything in Java?
Advertisement
It's the dereference operator, a little example will show you best.
struct somestruct{int a;int b;};int main(){somestruct* some = new somestruct;//access the members (*some).a = 10;//or using the operatorsome->a = 20;}
You mean the -> operator? You use it when you want to access a member of a struct or class instance via a pointer.

struct MyStruct{    int a;    char b;};int main(){    // Make an instance of MyStruct.    MyStruct theInstance;    // Get a pointer to that instance.    MyStruct* aPointerToTheInstance = &theInstance;    // Access the 'a' member directly.    theInstance.a = 74;    // Or access it via our pointer.    aPointerToTheInstance->a = 74;    return 0;}



P.S. Dammit!

[Edited by - Red Ant on June 27, 2006 6:58:55 PM]
It's a menace.
ps lol
I also forgot to say that there is nothing like it in java as it doesn't use pointers.
Quote:Original post by Anonymous Poster
ps lol
I also forgot to say that there is nothing like it in java as it doesn't use pointers.

No, Java uses references which basically are pointers. It's just that the language doesn't treat them like C++ does (actually it treats them quite differently)

Beginner in Game Development?  Read here. And read here.

 



it is used to access an element of a structure via a pointer to that structure.


x = structure_var.elementx; // access the struct variable directly

ptrx = &structure_var; // pointer to struct var ....


x = ptrx - > elementx;


pointers to objects and structs are passed to routines normally, so this kind of access is very common in c++


when the element of the struct is itself a pointer then you can see statements like this:

x = ptrx - > element_is_ptr - > element_of_that_one;



Quote:Original post by Alpha_ProgDes
Quote:Original post by Anonymous Poster
ps lol
I also forgot to say that there is nothing like it in java as it doesn't use pointers.

No, Java uses references which basically are pointers. It's just that the language doesn't treat them like C++ does (actually it treats them quite differently)


Not quite, try reseating a reference, try passing in a null reference.

Cheers
Chris
CheersChris
Thank you all very much =)
That makes code make SOOOO much more sense.

I'm sure you will get more nub questions from me on stuff that i have no idea how to even explain to google =P
Quote:Original post by Anonymous Poster
ps lol
I also forgot to say that there is nothing like it in java as it doesn't use pointers.


Java does use pointers, EVERY object in java is a pointer. They just dont call them pointers, and they use "." instead of "->".

A java "reference" is the equivilent of a C pointer, but a C reference has no equivilent in java.


If you are a Java programmer moving from its nice safe virtual machine into a real machine, please, please, PLEASE, go read a text book on operating systems and how stack/heap memory allocation works.
It will save you lots of pain later...

This topic is closed to new replies.

Advertisement