Zanima me sta su reference, zasto su uvedene u c++ i kakva je razlika izmedju reference i pointera?
Dobro bi doslo objasnjenje na primeru, kao i literatura(link) u kojoj je to objasnjeno.
Hvala
Referenca (alijas, upucivac...) je slicna pokazivacu, to je kao druga vrsta identifikatora,
stim da se stalno vezuje za taj deo memorije.
Uvedena je u C++ zbog lakseg koriscenja u odnosu na pointere,
pri prenosu parametara u f-ji po adresi(kad treba da vrate vrednosti)
lakse je ne dereferencirati stalno promenjivu (*p).
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.
"Verovatno da preko nje mnoge sile kontrolišu mnogo šta..." - GANDOR
"Kada bi ljudski mozak bio tako jednostavan da bismo mogli da ga shvatimo, onda bismo mi bili toliko glupi da ga ipak ne bismo mogli shvatiti."