Author Topic: C++ Pointers  (Read 4926 times)

0 Members and 1 Guest are viewing this topic.

Also use references when possible, they're safer than pointers.

This isn't entirely valid. If you're using smart pointers, you may be passing the smart pointer class, but the pointer is actually NULL!
(I know, minor, subtle point, but it's caught people before - not only that, sometimes a pointer makes more sense!)

Then your not actually dealing with the pointer, but the class instead  :P
And yes you can get a null pointer issue with references but it's something that doesn't come very easily, as compared to regular pointers.
That's cool and ....disturbing at the same time o_o  - Vasudan Admiral

"Don't play games with me. You just killed someone I like, that is not a safe place to stand. I'm the Doctor. And you're in the biggest library in the universe. Look me up."

"Quick everyone out of the universe now!"

 
Just pointing out :) I have heard people say that using references means that they're safe!
STRONGTEA. Why can't the x86 be sane?

 
Well it's a lot harder to do stupid mistakes (forgot to give it an address...etc) with references than with pointers.  Also references don't need to allocated and deallocated since the data they're pointing to is usually on the stack (unless you reference to a pointer [can you even do that?])
That's cool and ....disturbing at the same time o_o  - Vasudan Admiral

"Don't play games with me. You just killed someone I like, that is not a safe place to stand. I'm the Doctor. And you're in the biggest library in the universe. Look me up."

"Quick everyone out of the universe now!"

 

Offline Nuke

  • Ka-Boom!
  • 212
  • Mutants Worship Me
i still get hung up on pointers. seems every time i need to code something, like make a function return more than one value, and i know the only way to do that is with a couple of pointers in the arguments. but i still have to google it to figure out what symbols the dereference and address-of operators use. i always get those mixed up.:D

i know one thing that always confused me is that i figured the compiler reduced all variable names to an address anyway, so why would you use pointers over your typical named variables. of course its much easier to move around a memory address than it is to move around a several-byte struct. and or course c programmers use pointers regularly to make functions better at talking to whats calling them. but you really dont start appreciating pointers until you start getting into some more complicated areas of programming (such as object oriented code).
« Last Edit: October 27, 2010, 06:19:07 am by Nuke »
I can no longer sit back and allow communist infiltration, communist indoctrination, communist subversion, and the international communist conspiracy to sap and impurify all of our precious bodily fluids.

Nuke's Scripting SVN

 

Offline karajorma

  • King Louie - Jungle VIP
  • Administrator
  • 214
    • Karajorma's Freespace FAQ
You don't actually need pointers for OO. Java doesn't have them, for instance. That's cause in Java you always pass by reference anyway for anything except primitives (integers, floating points, etc). You can do much the same in C and just use references instead of pointers if you want.
Karajorma's Freespace FAQ. It's almost like asking me yourself.

[ Diaspora ] - [ Seeds Of Rebellion ] - [ Mind Games ]

 

Offline Sushi

  • Art Critic
  • 211
You don't actually need pointers for OO. Java doesn't have them, for instance. That's cause in Java you always pass by reference anyway for anything except primitives (integers, floating points, etc). You can do much the same in C and just use references instead of pointers if you want.

Technically, a reference is a kind of pointer. :)

EDIT: Apparently, it's actually the other way around. A pointer is a kind of reference variable:

Quote from: Wikipedia http://en.wikipedia.org/wiki/Reference_%28C%2B%2B%29
The name C++ reference may cause confusion, as in computer science a reference is a general concept datatype, with pointers and C++ references being specific reference datatype implementations.
« Last Edit: October 27, 2010, 09:53:53 am by Sushi »