Possible Duplicate:
dereferencing a pointer when passing by reference
Is the copy constructor called when you dereferencing a pointer when passing by reference to a function?
Here is a simple example
int& returnSame( int &example ) { return example: }
int main()
{
int inum = 3;
int *pinum = & inum;
std::cout << "pinum: " << returnSame(*pinum) << std::endl;
return 0;
}
My Guess what happeing:
When we dereference pinum we would expect the copy constructor to be called, but since the function is passed by value this will not be called?
If pinum copy constructor was called then a temporary object would be produced and the reference of that would be used, which would be very bad news in the form of undefined behaivour...
So what does happen...undefined behavior?
No, the copy constructor is not called.
The dereference operator creates an lvalue referring to the existing object. The reference parameter is bound to this existing object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With