Re: Pass a pointer variable to a function accept reference
Hi Michael:
1. test is a void function accepting an int lvalue
2. c is passed into test
Is that means i'm passing an int c, and temp is reference to c , eq int
&temp = c.
Let say for some reason, i must use pointer in main
int *ptr = new int(10);
and another function must accept an reference like void test(int &temp)
so passing ptr to test by using
test(*ptr);
will result passing as reference? is there any better way to do it? or this
is normal?
Many Thanks
L
"Michael Tsang" <miklcct@gmail.com> wrote in message
news:h9sh9a$1f7$1@news.eternal-september.org...
Louis wrote:
Hi all:
Im having a little bit confuse of passing a pointer variable to a
function
that accept reference:
void test(int &temp)
test is a void function accepting an int lvalue
{
temp+= temp + 2;
cout << "function:" << temp << endl;
cout << "function:" << &temp << endl; //c:0x22ff1c
}
int main(int argc, char *argv[])
{
int c = 0;
int *d = &c;
cout << "d:" << *d << endl;
*d is an int lvalue which is the same object as c
test(*d); // i guess, *d is dereference of d, which is value of c,
c is passed into test
and
same as int &temp = c, like passing reference?
cout << "c:" << &c << endl; //function:0x22ff1c,
return 0;
}
it works, i guess, *d is dereference of d, which is value of c, and same
as int &temp = c, like passing reference?
In the above code i'm trying to pass a dereference pointer d, which is
contain value of c, can anyone can explain to me if this is a good way to
pass a pointer variable?
Many Thanks
L