Const pass by reference question....
I was hoping that somebody could tell me why this works the way it does.
I have a class that has the data members as private and in order to access them I have public functions that return this data, as such:
Code:
private:
string ISBN;
string title;
string author;
string copyright_date;
vector<Book> Books;
public:
string display_title()
{
return title;
}
string display_ISBN()
{
return ISBN;
}
string display_author()
{
return author;
}
string display_copydate()
{
return copyright_date;
}
I have used an operator overload function to compare the ISBN numbers and return a bool. Effectively I want to pass in the two books without modifying them and do the comparison, however the function won't allow me to add const at the beginning of the parameter. This is the function I wrote:
Code:
bool operator==(const Book& a, const Book&b)
{
return a.display_ISBN() == b.display_ISBN();
}
As you can see it is calling the display_ISBN function, which I think is where the problem lies, but I am not sure why. When I pass by non-const it works as expected.
The error I get when passing by const reference is
error C2662: 'Book::display_ISBN' : cannot convert 'this' pointer from 'const Book' to 'Book &'
I would appreciate some advice as to why this is?
Thanks.