Hello,
I'm trying to get started with c++ by modifying some code from a K&P book that parses CSV data via cin, to read the data from a file. I am having great difficulty understanding the class constructor as it is implemented:
Code:
Csv(istream& fin = cin, string sep = ",") : fin(fin), fieldsep(sep) {}
I thought it would be as straightforward as replacing cin by a filepath but it doesn't work. I don't understand what the code after the colon is doing, especially fin(fin). The variables are defined as private:
Code:
private:
istream& fin; // Input file pointer.
string line; // Input line.
vector<string> field; // Field strings.
int nfield; // Number of fields.
string fieldsep; // Separator characters.
Could someone please break this constructor down and explain it. I have not seen this type of constructor anywhere else.
Thanks