私はちょうど質問があります:どのように+ =演算子をオーバーロードして文字列を返すことができますか?ここで私が試したことはありますが、成功はありません。+ =演算子をオーバーロードして文字列を返す方法は?
技術的に// 'Student' is the class that this function is in
// 'get_name()' returns the name of the student
// 'get_grade()' returns the grade of the student
// Description:
// Ultimately I will be creating a list of students and their grades in
// the format of (Student1, Grade1) (Student2, Name2) ... (StudentN, GradeN)
// in a higher level class, and thus I need an overloaded += function.
Student& Student::operator+=(const Student& RHS)
{
string temp_string;
temp_string = "(" + RHS.get_name() + ", " + RHS.get_grade() + ") ";
return temp_string;
}
戻り値の型を 'std :: string'に変更しますか? – NathanOliver
@ NathanOliverどのようにそのアイデアを考え出しましたか? o_o – DeiDei
これは混乱して予想外になることに注意してください。現在のオブジェクトを変更していないようです。私はこれをしないことを非常に提案します。 – Falmarri