PDA

View Full Version : some c++ questions



sherlockmeister
03-15-2008, 02:52 AM
was reading tuts on cplusplus.com and they don't really clear up a few things so...

what's the difference between a reference and a pointer (actually whats the point of references since pointers can clearly do everything a reference can and more (pointer arithmetic, etc))?

actually thats all the questions i can think of for now

by the way anyone else think c++ is freaking sweet? so much more fun than java...

edit: i guess references take less room than pointers??

1337.
03-23-2008, 03:28 PM
Hey dude, I don't know C++ (but I'm going to start learning it soon), I'm always interested in learning new things about other languages though.

Did a little searchign and found this very helpful link:
http://www.parashift.com/c++-faq-lite/references.html



When should I use references, and when should I use pointers?

Use references when you can, and pointers when you have to.

References are usually preferred over pointers whenever you don't need "reseating". This usually means that references are most useful in a class's public interface. References typically appear on the skin of an object, and pointers on the inside.

The exception to the above is where a function's parameter or return value needs a "sentinel" reference — a reference that does not refer to an object. This is usually best done by returning/taking a pointer, and giving the NULL pointer this special significance (references should always alias objects, not a dereferenced NULL pointer).

Note: Old line C programmers sometimes don't like references since they provide reference semantics that isn't explicit in the caller's code. After some C++ experience, however, one quickly realizes this is a form of information hiding, which is an asset rather than a liability. E.g., programmers should write code in the language of the problem rather than the language of the machine.

Hope that helps :)

1337.

sherlockmeister
03-25-2008, 01:25 AM
wow, freaking awesome dude thanks a lot