Idan Cohen
Idan Cohen

Reputation: 135

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!

Upvotes: 1

Views: 85

Answers (1)

Rabbid76
Rabbid76

Reputation: 211278

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;

Upvotes: 3

Related Questions