Reputation: 15
I have a class called Airplane with a private data member thats a char array.
// char array variable in my class
char* name{nullptr};
my goal is to compare for equality this variable and an input variable of type const char [].
my overload function looks like this:
bool Airplane::operator==(const char input_name[]) const{
if (this->name == input_name) {
return true;
}
return false;
}
By overloading the == operator, I want to be able to do the following:
Airplane plane("hello");
if (plane == "hellooo") {
// do something
}
I want to be able to create a class with a text variable like "hello" and then be able to == it to any random text I want to compare equality. Right now my code just doesnt work, it runs and then ends in the console with no errors. basically I need to compare to char arrays, one within the class the overload function is built in, and another given as input by the user. Thank you for any help.
Upvotes: 0
Views: 54
Reputation: 2238
As @PaulMcKenzie has rightly said, char*
is not an array.
I suggest using std::string
instead of char*
as well as in overload
as follows:
const bool Airplane::operator==(const std::string& str_to_be_compared) const {
return this->(whatever variable stores the name of the plane) == str_to_be_compared;
}
Upvotes: 1