I have a program I am working on that requires us to overload the >> for the Employee class so that when it’s used it reads in the right type of employee so I have something like this in my main program:
Employee *emp;
empIn >> emp;
I figured the base class is where I would want to do it, because it is the only one that applies to all of the derived classes. The type is determined by an integer at the start of the line so I figured that something like this might work (as I don’t know the type until I read it):
istream &operator >> (istream &stream, Employee &emp)
{
int type;
stream >> type;
switch(type){
case 1:
*emp = new Hourly;
break;
...
}
return stream;
}
But, it doesn’t work. I’m I going about this the right way? And if not, please point me in the right direction.
The second argument to your istream function should be
Employee* &empif you want to be able to assign to the pointer as in your example usage. The type would then be a reference to a pointer, which lets you assign it as you want to do instead the method.