Total members 10249 | Gratitudes |It is currently Thu May 17, 2012 8:58 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Code Snippet
 Code subject: Constant References
PostPosted: Thu Nov 13, 2008 2:17 pm 
Offline
Beginner
User avatar

Joined: Sun May 25, 2008 5:34 pm
Posts: 95
Has thanked: 2 time
Have thanks: 1 time

Code:
#include <iostream>

using namespace std;

// This simply sets a to a function of b.
void fred(int &a, const int &b)
{
        a = 2 * b * b;

        // This would be illegal:
        //   b = 10;
}

main()
{
        int m;

        fred(m, 17);
        cout << "m = " << m << endl;

        int c = 45;
        fred(m, 2*c + m);
        cout << "m = " << m << endl;
}


The const modifier on a reference parameter means that the function will not change the item referred to.

Note that it is allowed to pass an expression parameter to a const reference, since it will not be assigned.

We'll find more use for this feature later, when we start flinging around things larger than integers and floats.


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  

 Similar topics
 Functions and References
 Boolean type constant
 References confuse me. How do they differ from pointers?

All times are UTC [ DST ]


Users browsing similar codes

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Code Snippet | Next Code Snippet 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team