Jump to content
  • Advertisement
Sign in to follow this  
fubz

C++ '-'

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

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?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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 operator
some->a = 20;
}

Share this post


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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
ps lol
I also forgot to say that there is nothing like it in java as it doesn't use pointers.

Share this post


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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster


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;



Share this post


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

Share this post


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

Share this post


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

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!