我只是有一個快速問題:我如何重載+ =運算符來返回一個字符串。這是我嘗試過的,但沒有成功。如何重載+ =運算符來返回一個字符串?
// '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