| azrielcross (3) | |||||
|
Hello Everyone, I understand the basics of pointers and classes, but there is still a lot I don't comprehend. I understand that passing a parameter by reference is far more efficient than passing it by value... but this leads me to several questions: 1). Why ever pass a parameter by value? Given pointers you can either leave original data stand, or access it directly for change... if this is all faster and more efficient, what's the use of byval anymore? 2). I've played with the following code, and clearly when I compile there are huge differences between them, but I don't understand the reasoning why:
and...
Why is the second so badly formed? Forgive my questions, they may be pretty beginner and confusing, but these points will really help me make it to the next step in fully understanding these concepts. Thanks Az | |||||
|
|
|||||
| guestgulkan (2830) | |||||
Should be like this:
| |||||
|
|
|||||
| Zaita (2301) | |
|
As for your question no 1. 1) Because references are typically implemented by C++ using pointers, and dereferencing a pointer is slower than accessing it directly, accessing values passed by reference is slower than accessing values passed by value. 2) Security. You don't always want your value changed. So it's safer to pass it by value that way you don't have to worry about it being changed. This is especially importing in multi-threaded environments when you need thread-safe code. | |
|
|
|