A
arnuld
it works fine:
/* C++ Primer - 4/e
*
* example from section 7.2.2, pointer-swap
* STATEMENT
* in a function call where parameters are pointers, we actually copy
the pointers.
* here in this example we are using the original pointers.
*
*/
#include <iostream>
void pointer_swap(int*& rp1, int*& rp2)
{
int* temp = rp2;
rp2 = rp1;
rp1 = temp;
}
int main()
{
int i = 1;
int j = -1;
int* pi = &i;
int* pj = &j;
std::cout << "before swapping pointers:\t*pi: "
<< *pi
<< "\t*pj: "
<< *pj
<< "\n\n";
pointer_swap( pi, pj );
std::cout << "after swapping pointers:\t*pi: "
<< *pi
<< "\t*pj: "
<< *pj
<< std::endl;
return 0;
}
the only thing i do not understand is the line "int* temp = rp2;".
lvale is a pointer-to-int and rvalue is a reference-to-pointer-to-int,
they are two different types. why the expression work then ?
/* C++ Primer - 4/e
*
* example from section 7.2.2, pointer-swap
* STATEMENT
* in a function call where parameters are pointers, we actually copy
the pointers.
* here in this example we are using the original pointers.
*
*/
#include <iostream>
void pointer_swap(int*& rp1, int*& rp2)
{
int* temp = rp2;
rp2 = rp1;
rp1 = temp;
}
int main()
{
int i = 1;
int j = -1;
int* pi = &i;
int* pj = &j;
std::cout << "before swapping pointers:\t*pi: "
<< *pi
<< "\t*pj: "
<< *pj
<< "\n\n";
pointer_swap( pi, pj );
std::cout << "after swapping pointers:\t*pi: "
<< *pi
<< "\t*pj: "
<< *pj
<< std::endl;
return 0;
}
the only thing i do not understand is the line "int* temp = rp2;".
lvale is a pointer-to-int and rvalue is a reference-to-pointer-to-int,
they are two different types. why the expression work then ?