When I try to compile the code
istream in;
if (argc==1)
in=cin;
else
{
ifstream ifn(argv[1]);
in=ifn;
}
gcc fails, complaining that operator=
is private. Is there any way to set an istream
to different values based on a condition?
You could use a pointer for in
, e.g.:
istream *in;
ifstream ifn;
if (argc==1) {
in=&cin;
} else {
ifn.open(argv[1]);
in=&ifn;
}