[C++]What is pass by reference? What is pass by value? Why do you sometimes need to pass by reference and other times pass by value? What is a Method?
When you pass by reference - you're passing some pointer to a block of memory. So, the thing getting copied around is just the memory address where the real information is held. When you pass by value, the actual block of data is getting copied around every time you need it somewhere new. So, if you have a string that is passed-by-value into a function, the entire contents of that string are copied into that function's memory space before the function runs. Finally a method is a class member that is composed of executable code. So, you could say it's a function that is a member of a class.
Thanks.... So, you only pass by reference when you need to reveal or manipulate the memory address?
No, not necessarily. You pass by reference if you want multiple functions to be referring to the same exact object. In C++, there are in fact two ways to do pass by reference: one is actually called a reference, and does not give you access to the memory address, it just lets you refer to the same object, while the other is called a pointer, and gives you access to the value of the memory address.
Can you give me a example of when you might need to use pass by reference instead of pass by value?
Honestly, anytime you're passing objects in C++, it's often a good idea to pass by reference. Not always, mind you, but if you don't, you'll find yourself copying the objects. Copying objects is, first of all, much more expensive than just passing a reference. Second of all, it's a bit unpredictable, in that the flow for copying an object is a bit weird. There's a default copying mechanism, plus you can write your own copy constructors that will let you refine how a given object is copied. There are complications everywhere. When in doubt, declare a function parameter in C++ as a const T &, where T is the type you're passing. This is a const reference. You can use just as if you had a copy, except that if you try to use any methods on it that modify its contents, you'll get an error. This ensures that (a) you aren't copying the object (cheaper) and (b) you aren't modifying it (which could be unexpected). More specifically, const T & means you can't invoke any methods of T that aren't declared as const. Const methods are typically accessors (functions that return the value of an instance variable) and are not allowed (by the compiler) to modify the object's own instance variables. I know that's pretty complicated, but unfortunately references and consts and copying are a very deep and occasionally confusing part of C++.
Hmm I will try to go over some examples in my book. I hope to spend some more time actually learning the concepts and understanding the language very soon.
I would pass by reference if I wanted my function to be able to alter a passed value and have the alteration stay after the function finishes. I would also pass by reference it I was passing a large data structure to a function.
Join our real-time social learning platform and learn together with your friends!