Use Pointer or Reference?
A or B is better? Why it is better?
A.)
int num =10;
aaa(num);
void aaa(int &temp)
{
.........
};
B.)
int num =10;
aaa(&num);
void aaa(int *temp)
{
.........
};
Thx.
A is better. However, A is not _always_ better.
In this specific case, A is better because all you want to do is pass the value of a construct to a function without creating a local copy (and also optimize the stack manipulation). Fine. If, however, you wanted to pass an array you would want B (note that arrays are always passed by reference anyways). The difference lies in the internal processing:
Note the extra ''*'' in bbb(); it''s necessary to access the value in *tenp rather than the pointer temp. This can be very useful when manipulating arrays and other contiguous memory regions (C-style strings, for example).
Hope that was actually helpful.
[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
In this specific case, A is better because all you want to do is pass the value of a construct to a function without creating a local copy (and also optimize the stack manipulation). Fine. If, however, you wanted to pass an array you would want B (note that arrays are always passed by reference anyways). The difference lies in the internal processing:
void aaa(int &temp){ // double temp temp *= 2;}.void bbb(int *temp){ // double temp *temp *= 2;}
Note the extra ''*'' in bbb(); it''s necessary to access the value in *tenp rather than the pointer temp. This can be very useful when manipulating arrays and other contiguous memory regions (C-style strings, for example).
Hope that was actually helpful.
[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
Thank you!
Are you mean that if i pass array to a function, i am better use pointer than reference?
But i don''t understand that:
"In this specific case, A is better because all you want to do is pass the value of a construct to a function without creating a local copy "
Pointer is also without creating local copy. what are the differents between Pointer and Reference in this case?
Thanks.
Are you mean that if i pass array to a function, i am better use pointer than reference?
But i don''t understand that:
"In this specific case, A is better because all you want to do is pass the value of a construct to a function without creating a local copy "
Pointer is also without creating local copy. what are the differents between Pointer and Reference in this case?
Thanks.
B is bad, A is worse, a-mon-avis.
A)
int num=10;
aaa(num);
void aaa(const int& temp)
{
//don't twiddle temp
}
Unless you're overloading operators, use constant references. If you're going to twiddle it, tell the user by requiring a pointer to it.
B)
int num=10;
num = aaa(num);
int aaa(int temp)
{
//twiddle temp
return temp;
}
Int are small, so copying them is less expensive than using pointers to them. If you want to twiddle a structure or a class, add a method to the construct.
Magmai Kai Holmlor
"Oh, like you've never written buggy code" - Lee
"What I see is a system that _could do anything - but currently does nothing !" - Anonymous CEO
Edited by - Magmai Kai Holmlor on December 9, 2001 3:55:37 AM
A)
int num=10;
aaa(num);
void aaa(const int& temp)
{
//don't twiddle temp
}
Unless you're overloading operators, use constant references. If you're going to twiddle it, tell the user by requiring a pointer to it.
B)
int num=10;
num = aaa(num);
int aaa(int temp)
{
//twiddle temp
return temp;
}
Int are small, so copying them is less expensive than using pointers to them. If you want to twiddle a structure or a class, add a method to the construct.
Magmai Kai Holmlor
"Oh, like you've never written buggy code" - Lee
"What I see is a system that _could do anything - but currently does nothing !" - Anonymous CEO
Edited by - Magmai Kai Holmlor on December 9, 2001 3:55:37 AM
Mm.....i know the following program will has same result.
A)
void aaa(int &temp){ temp++ };
int num =10
aaa(num);
cout<
B)
void aaa(int *temp){ (*temp)++ };
int num =10;
aaa(&num);
cout<
My question is what are the differents between them and which one is better?
If the num variable is changed to a large object, which one will run faster and space saving?
A)
void aaa(int &temp){ temp++ };
int num =10
aaa(num);
cout<
B)
void aaa(int *temp){ (*temp)++ };
int num =10;
aaa(&num);
cout<
My question is what are the differents between them and which one is better?
If the num variable is changed to a large object, which one will run faster and space saving?
Use [ source] [ /source] tag to surround code, or use & l t (without the spaces) for a less-than symbol.
(& g t for >)
Neither is faster, they type doesn''t matter, it''s just a different way to write the same thing. It''s my opinion that references are prefered to be const (the expection is operator overloading, where you use non-const references to make the operators work like they are supposed to)
Magmai Kai Holmlor
"Oh, like you''ve never written buggy code" - Lee
"What I see is a system that _could do anything - but currently does nothing !" - Anonymous CEO
(& g t for >)
Neither is faster, they type doesn''t matter, it''s just a different way to write the same thing. It''s my opinion that references are prefered to be const (the expection is operator overloading, where you use non-const references to make the operators work like they are supposed to)
Magmai Kai Holmlor
"Oh, like you''ve never written buggy code" - Lee
"What I see is a system that _could do anything - but currently does nothing !" - Anonymous CEO
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement