If an object is passed by value,then the value is copied to the object with the name of the parameter.Why should a reference be passed instead, when the type of an object is any of the : ifstream , ofstream , istream , ostream?
unsigned int number_of_lines(ifstream file) {
string line;
unsigned int lines=0;
while(getline(file,line))
++lines;
return lines;
}
In the code above, if the interface changes to:
unsigned int number_of_lines(ifstream& file)
Everything runs fine. Why is it obligatory to pass a reference?
Thanks!