shuisheng said:
Dear all,
Would you please tell me when using pointer and not using pointer,
which is faster to access data? Such as
float *pVal
float val
MyClass *pA pA->member1
MyClass a a.member1
Thanks,
Shuisheng
Probably doesn't make any difference in most cases since under-the-covers
the two mechanisms would generate similar code.
The real question would be the stylistic one. Both pointers and
references model aliases to other objects,
with the case of pointer, the object might be nil, ie, it can be missing,
whereas with a reference it must represent a valid object.
If you use a reference, it is unecessary to check for the validity of the
object being referenced, it must exist ( unless you've done some tricky
things to sabotage this invariant): ie,
void doFoo( Foo& foo )
{
// do something with foo..
}
void doFoo(Foo* foo )
{
if ( foo != 0)
{
// do something with foo.
}
}
I'd prefer to use the reference when I can since it make my code clearer and
also eliminates some unnecessary checking.