C++ c+中的重载运算符+;
我想重载将在此代码上工作的“=”运算符:C++ c+中的重载运算符+;,c++,C++,我想重载将在此代码上工作的“=”运算符: 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 opera
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());
}
void toArr(AVLNode student,student*studentArr){
int studentID=student.data;//这就是我要使用该运算符的地方
....
}
int运算符=(int number,const Student&Student){//Error:'int运算符=(int,const Student&)'必须是非静态成员函数
学生tmp=学生;
return(tmp.getID());
}
tmp.getID()
是一个int
。
这可能吗
仅供参考,我搜索相同的问题,但未找到具有2个参数的问题
谢谢 您需要的是在您的类中使用一个cast操作符
Student
来键入int
:
class Student
{
public:
int id;
int getID() const { return id; }
operator int() const
{
return getID();
}
};
Student student;
int id = student;
你想要什么?向我们展示此运算符的用例我认为您正在寻找的是转换–它可能是
operator int()const{return this->getID();}
?在“void”函数中,您可以看到其用法。我想将“tmp.getID()”分配到“studentID”中