简体   繁体   中英

Overload operator in c++

I want to overload the "=" operator that will works on this code:

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() is an int . Is this even possible?

FYI I search for the same problem but didn't find one with 2 arguments..

Thanks!

What you need is a cast operator in your class Student to type int :

class Student
{
public:

    int id;
    int getID() const { return id; }

    operator int() const
    {
       return getID();
    }
};

Student student;
int id = student;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM