我想重载将对此代码起作用的“=”运算符:
void toArr(AVLNode<Student> student, Student* studentArr){
int studentID = student.data; //This is where I want to use that operator
....
}
int operator=(int number, const Student& student){ //Error: 'int operator=(int, const Student&)' must be a nonstatic member function
Student tmp = student;
return (tmp.getID());
}
tmp.getID()
是int
。
这甚至可能吗?
仅供参考我搜索同样的问题,但没有找到一个有2个参数的人。
谢谢!
答案 0 :(得分:3)
您需要在班级Student
中使用强制转换操作符来键入int
:
class Student
{
public:
int id;
int getID() const { return id; }
operator int() const
{
return getID();
}
};
Student student;
int id = student;