こんにちは、istream& operator>> と混同しています。C 文字列の動的メモリ割り当てを使用しているクラスの入力を取得するには、この演算子をオーバーロードする必要があります。
私のEmployee.hファイルは
#include <iostream>
using namespace std;
const double MIN_WAGE = 10.25;
class Employee {
int num;
char * name;
double rate;
public:
Employee();
Employee(const Employee&);
Employee operator=(const Employee&);
friend istream& operator>>(istream& is, Employee& employee);
friend ostream& operator<<(ostream& is, const Employee& employee);
friend bool operator>(const Employee& a, const Employee& b);
~Employee();
};
代入演算子を呼び出すコピーコンストラクターがあります
Employee::Employee(const Employee & e) {
name = NULL;
*this = e;
}
Employee Employee::operator=(const Employee & e) {
if (this != e) {
num = e.num;
rate = e.rate;
if (name != NULL) delete [] name;
if (e.name != NULL) {
name = new char[strlen(e.name) + 1];
strcpy(name, e.name);
}
else name = NULL;
}
return *this;
}
また、代入演算子では、使用している C 文字列の長さに対してメモリを動的に割り当てました。これまでの私の istream 関数:
istream& operator>>(istream& is, Employee & e) {
int n;
double r;
}
私の質問は、istream 関数の代入演算子で新しい動的メモリ割り当てを使用するにはどうすればよいですか?