Everyone
BAN USERHmm.
QTH is Nagpur, in Central India.
I was employed as a programmer until I left. Between jobs now, and looking.
The difference is that copy by value means the method which is called may not modify the content received. OTOH, copy by reference means a reference is passed so that any operations performed affect the referent.
The code below is off the top of my head, so it may not compile... but it'll probably serve as a demonstration.
The language author has covered your question on his site - http://www.research.att.com/~bs/bs_faq2.html
#include <iostream>
void byValue( int n )
{
std::cout << "1. In byValue: " << n << std::endl; // initial value
n = 32;
std::cout << "2. In byValue: " << n << std::endl; // updated value
}
void byReference( int& n )
{
std::cout << "1. In byReference: " << n << std::endl; // initial value
n = 32;
std::cout << "2. In byReference: " << n << std::endl; // updated value
}
int main(int argc, char* argv[])
{
int x = 5;
std::cout << "Before byValue: " << x << std::endl; // initialize
byValue( x );
std::cout << "After byValue: " << x << std::endl; // no change
std::cout << "Before byReference: " << x << std::endl; // initialize
byReference( x );
std::cout << "After byReference: " << x << std::endl; // yep, changed!
return 0;
}
An attempt is being made to bind a reference to an integer to a pointer to an integer... That can't be right. It'll probably fail to compile
- Everyone February 21, 2007