Pointers?

closed account (17poGNh0)
I see -> in code all the time. Is it not easier to just avoid them? Wht are the advantagez?
If you have an instance of a class object for class Thing, say:

 
int count = Thing.count();


If you have a pointer to a Thing object:
 
int count = Thing->count();


There are no particular advantages to the syntax. Generally I use references to class instances instead of pointers, though.
Say you have a pointer to a class, and say that class has a pointer to another class, and you need to access a member of that inner class. You could use something like the following:

(*(*classouter).classinner).member

...but I'm pretty sure that many programmers would agree with me in saying that this doesn't look very nice. Instead, you could use the -> operator to express the same thing like this:

classouter->classinner->member

Isn't that a lot easier to read? :) You can think of it like the . operator except for pointers to classes.

-Albatross
Last edited on
closed account (17poGNh0)
Altbat, Why use pointers at all???
closed account (17poGNh0)
I agree with Texan, is there a point??!?!?
Your question essentially tells us that you're very inexperienced. That's no crime; what does seem foolish is to think that because you don't use them, they are useless.

You use pointers:

- To control object lifetime yourself beyond local scope.
- When you need to make an object that is too big to fit on the stack (very common, especially workig with large files such as images).
- To implement polymorphism.
- More.

closed account (17poGNh0)
thankyou Moschops. you make more good point.
Topic archived. No new replies allowed.