Const functionality

Why following declaration gives error:
bool operator () (student& x, const student& y) ?

Your question is not clear enough. Please copy and paste the whole question or code so that i can check.

Study the following code carefully.

struct student{
string name;
float cgpa;
student(string name, float cgpa){
this -> name = name;
this -> cgpa = cgpa;
}
};

struct comp{
bool operator () (const student& x, student y) {
return x.name < y.name;
}
};

set < student, comp > batch;
The above code compiles successfully. Choose which of the following declarations of the function operator () will give a compilation error.

bool operator () (student x, student y)

bool operator () (const student& x, const student& y)

bool operator () (student& x, const student& y)

None of these

When you are sending by reference (by using & operator) to a custom comparator it must be sent with const type. const means that it will become immutable.
bool operator () (student& x, const student& y) will give error as the first parameter is not with a const.

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.