Home
Username:
Password:
C++ Fundamentals Tutorials

Types & Arguments / Reference Types




Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.


Learn More

Subtitles of the Movie

This is Tim Heagarty with the Virtual Training Company we're looking at C Plus Plus Fundamentals. We have a new C Plus Plus item called a reference and a reference is another way to point to a variable. Now we know pass by value and pass by reference in function calls and that's the way we used to determine in C well its kind of similar now in C Plus Plus but we have a thing now called a reference. And a reference is actually just another way to name a variable or talk about a variable. But when you pass a reference to a variable its just like passing a pointer to it and you can deal with it in a function, modify it and that modifies the real variable and not just the value. If you remember in C if you wanted to change a value in a variable you would either pass the address of the variable to your function, work on it and then that would change the value, the variable. Or you had to send the value into the function and then return back your modified value and have that assigned into the original variable. Well now we have a thing called a reference, one thing to take note of is that operators do not really work on a reference, they work on what the reference refers to on the original variable. The referenced variable is affected by the operator, not the reference itself. Let's take a quick look at a piece of code here and here we have an integer I, set equal to a 100, no big deal. And here we have a reference we use the ampersand and it looks kind of like address of, well this is an integer ampersand, this is an integer reference and that's going to be our eye so that is a reference to I. A reference when it is declared has to be initialized. So you can't just come up with int address, or int reference R-I, see I even want to say address. But int reference R-I without setting it equal to something, because a reference is no good unless it has something to refer to. It's actually quite illegal to have a reference without having it initialized, so we have to initialize it. Then we will put out the value that is an I and then we will increment the reference. Well remember, not really. We are not really incrementing the reference, when you increment a reference you actually increment the original variable and then we'll put out I again after we see the reference has been incremented. So let's fire this off and you can take a look at it and we should get 100 and 101, yup there we go, 100 and 101, its exactly what we thought we were gonna get and all because we incremented a reference to a variable and operating on the reference really operates on the variable. This is most powerful when you make function calls and you need to pass by reference. Now you can do it officially, you don't have to pass the address and all the things that we used to fudge in C to get that job done. So now we pass by reference.

Tutorial Information

Course: C++ Fundamentals
Author: Tim Heagarty
SKU: 33797
ISBN: 1-934743-09-7
Release Date: 2007-09-14
Duration: 4 hrs / 55 lessons
Captions: For Online University members only
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available