Reputation: 183
In C when we want to input an integer for example, we can use scanf()
.
The variable to which our input is to be saved is passed as a pointer, and this makes sense for the function needs the address for this variable otherwise the inputted value would lost as soon as the function returned.
How can (in C++) cin>>x
, which I understand to be cin.operator>>(x)
work if I don't pass an address to it?
Upvotes: 0
Views: 95
Reputation: 1
"How can C++ cin>>x , which I understand to be cin.operator>>(x), work if I do not pass an address to it?"
It's because the signature of the
std::istream& operator>>(std::istream& is, <type>& obj);
// ^
function takes a reference parameter for obj
.
A C++ reference somehow replaces passing pointers to supplied parameters around, but also needs to be initialized with a valid address.
That's implemented for any primitive types, and some of the complex types (as e.g. std::string
) of the standard library.
Upvotes: 2