<NOSCRIPT><A href="http://about.com/" target=_blank><IMG src="C++ Tutorial - Lesson 13 References_files/0.gif" border=0></A></NOSCRIPT>   About > Computing & Technology > C/C++ 
Search     
 Hi, I'm John Kopp, your guide to C, C++ and C# programming. This site provides what you need to know to learn C, C++ or C# programming. Browse the subjects on the left, try one of the tutorials or use the search box to get started.

C/C++

with John Kopp
Your Guide to one of hundreds of sites
 Home · Articles · Forums · Chat · Newsletters · Help    
Subjects

  ESSENTIALS
· C Tutorial
· C++ Tutorial
· C/C++ Glossary
· Tips
· More Tutorials
  BUYER'S GUIDE

Product Reviews
Top Picks
C Tutorials
C++ Tutorials
C#
Advanced C
Advanced C++
Beginning C
Beginning C++
Books
C++ Builder
Careers
CGI
CM
Dictionaries
CompilingDebugging
Freeware/Shareware
Humor
Magazines
OOAD
Polls
SoftwareEngineer'n
Style
STL
UNIX/GNU
Visual C++

Subject Library

All articles on this topic

 

Stay up-to-date!
Subscribe to our newsletter.

Web Hosting
Global Servers

 
 
 
Advertisement
> Free Credit Report
 
C++ Tutorial - Lesson 13: References
 More of this Feature
• References, Page 1
• References, Page 2
• References, Page 3
 Related Resources
• C++ Tutorial - Introduction to Pointers
• C++ Tutorial - Functions
• Advanced C++ Tutorials
• Simple Solutions in C++
• C++ Programming Tips
• All C++ Lessons
 
 From Other Guides
• Emoticons 101
 

References
Good references are required to get a job, loan or possibly even a date. You should never lie about references on your resume because they might be checked. What does this have to do with C++? :-? Nothing, but I can't help but create bad puns, and it gave me an opportunity to use the "puzzle mouth" emoticon. In C++, a reference is an alias to a variable. The real usefulness of references is when they are used to pass values into functions. They provide a way to return values from the function, as will be shown in the next lesson. For now, we'll examine how to create and use references.

A reference is an alias to an object. Here's some code that shows how to create a reference.

int val;    // Declares an integer
int &rVal = val;
        // Declares a reference to the integer object val

Notice the use of the "&" before the reference name. In this context, the "&" is called the reference operator. It indicates that rVal is a reference rather than an ordinary object. In earlier lessons, the "&" was used to obtain an address of an object. In that context, the "&" was called the address of operator. The address of operator is used to obtain an address, typically, to initialize a pointer. Here's an example.

int val;    // Declares an integer
int *pal = &val;
        // Declares a pointer and initializes it to the address of val.

A reference is not a unique object. It is merely an alias or synonym for another object. The reference identifier (name) may be used anywhere the referred identifier may be used. Any changes to the reference also apply to the original object. Changes to the original object are also seen through the reference.


#include <iostream>
using namespace std;

int main()
{

    int val = 1;
    int &rVal = val;

    cout << "val is " << val << endl;
    cout << "rVal is " << rVal << endl;

    cout << "Setting val to 2" << endl;
    val = 2;

    cout << "val is " << val << endl;
    cout << "rVal is " << rVal << endl;

    cout << "Setting rVal to 3" << endl;
    rVal = 3;

    cout << "val is " << val << endl;
    cout << "rVal is " << rVal << endl;

    return 0;
}


Next page > References, Page 2 > Page 1, 2, 3


Subscribe to the C/C++ Newsletter
Name
Email






Email this page | Sign up for a Newsletter |
Explore More
Most Popular Articles
• C++ Programming Tutorial
• C Programming Tutorial
• C++ Tutorial - Lesson 12: File Input and Output
• C++ Tutorial - Lesson 4: Input and Output - cin, cout, iostr...
• C++ Tutorial - Lesson 10: Arrays and Vectors
• C++ Tutorial - Lesson 1: Writing and Compiling A First Progr...
• Tutorials
What's Hot Now
• Teach Yourself C++ in 21 Days by Jesse Liberty
• C Programming Tutorial
• C++ Tutorial - Lesson 25: Operator Overloading
• The C Programming Language, 2nd Edition by Kernighan and Rit...
• Topics in C++: Function Template Specialization
• C Programming Tips: Using Long Constants
• C Tutorial - Lesson 5: Conditional Processing, Part 1 - Rela...
About Us | Advertise on This Site | User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2004 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.