Reputation: 48096
I'm writing a program with three levels of inheritance. It is required that I overload the operator==
in the base class, and then override that function in the derived classes (I am not allowed to change this design).
The function is a bool which does a memberwise comparison of two objects. How do I process supers return value? In Java getting this value is simple: I just make the first line of the overriden function a call to super, for example:
retValue = super.myFunction();
I need to achieve this same result, but in C++ and with awful operator overloading. Some pseudo code for the overrloaded function would be much appreciated.
I also have another question relating to this; What will happen if the operator is used on instances from different subclasses? For example sub1 and sub2 inherit from base, which version of the function will be executed for the following line of code in Main:
if (sub1 == sub2)
Will it generate a compiler error?
Edit: Firstly, I am a student so I cannot write the code to accomplish this task in the way I would like to. The program requirements state;
"Vehicle operator overload: the == operator
The Vehicle class needs to override the == operator. Two objects are equal if all of their data fields are equal and if their passenger lists are identical. This needs to be defined in the base class and then overridden in the derived classes."
I don't really have any code to post because in reading Billy's response I realized that I had actually written the operator== function free of the class (which won't allow me to override it, and won't meet the program requirements).
The example;
if (sub1 == sub2)
was entirely hypothetical. I will actually have an array of Vehicle objects, I have a Car and Airplane class which inherit from Vehicle, and then a MagicSchoolBus which inherits from Car and an Xwing which inherits from Airplane. In the Vehicle class I have an int 'vType_' which identifies the specific subclass each Vehicle instance belongs to. To prevent a runtime crash I will simply check to make sure the objects have the same vType_ prior to using the == operator on them.
As much as I would to prefer to write a compare function, I am not allowed to. I have to find a way to use the return value from the operator== function in the base class because it is comparing several data members, and I will of course be graded down if I rewrite this code in each of the derived classes.
If I can't use that return value I either have to rewrite code, or I will get the wrong result when an object has different values in members of the base class but the same values in the derived class.
I'm going to have to rewrite this function as a member of the Vehicle class but I will post it anyways in hope that it may be useful. Each of the derived classes have a few more members specific to those types of vehicles, and an array of Passenger objects which will have be compared.
// equality operator overloaded to do a memberwise comparison
bool operator== (Vehicle& obj1, Vehicle& obj2) { bool retValue = false;
if (strcmp(obj1.getName(), obj2.getName()) == 0)
{
if (obj1.getType() == obj2.getType() && obj1.getFuel() == obj2.getFuel())
{
if (obj1.getRate() == obj2.getRate() && obj1.getStatus() == obj2.getStatus())
{
retValue = true;
}
}
}
return retValue;
}
Upvotes: 0
Views: 615
Reputation: 178461
because C++, unlike Java, supports multiple inheritence, you will need to explicitly specify which super you need to use by SUPER::operator==()
(replace SUPER by the real super class name..)
Upvotes: 3
Reputation: 106549
It is required that I overload the == operator in the base class, and then override that function in the derived classes
That seems like an extremely strange thing to do. Operator overloads should generally be free functions, not class members, where possible. Could you post some code describing what you want to do more specifically? Because a general implementation of what you'd want is not possible. E.g. if you compare Derived 1 and Derived 2 through a base class pointer, how would the compiler know which operator==
to call? Both Derived 1 and Derived 2 implement that function, but there'd be no way to choose. The derived classes should just implement == hiding the implementation of the base class, in the few cases where someone might need to do something like that.
Don't mix operator overloading with virtual functions -- it's painful and it's not how operator overloading is designed to operate.
How do I process supers return value?
You don't. There's no such thing as a "super" in C++ (for one thing, if you inherit from two classes, how is the language supposed to know what the base version is? :P). If you want to call the overload of the base class, you call the base class function directly. Example:
class SomeBaseName
{
int aMember;
public:
bool operator==(const SomeBaseName& rhs)
{
return aMember == rhs.aMember;
};
};
class Derived : public SomeBaseName
{
int anotherMember;
public:
bool operator==(const Derived& rhs) //Note that this is not a virtual function, it is *hidden* instead.
{
if (!(SomeBaseName::operator==(rhs))
return false;
return anotherMember == rhs.anotherMember;
}
};
I really really recommend you not do that though.. it makes no sense to do things that way with operator overloading.
What will happen if the operator is used on instances from different subclasses? For example sub1 and sub2 inherit from base, which version of the function will be executed for the following line of code in Main
Neither will be called. The call is ambiguous and will cause a compile-time error.
Finally, one last piece of advice: Don't try to learn C++ in terms of Java. Learn C++ as a new language. They are different languages -- moreso than their similar syntax would suggest. C++ allows you do do several things for which there is absolutely no equivalent in Java (e.g. pointer arithmetic, operator overloading, multiple inheritance, template metaprogramming, etc.), and while there are some things that are similar in places, idiomatic examples in both languages are very different from each other. If you think of things as the Java equivalents in places you are going to be confused when the languages differ or when C++ piles concepts onto it's (very dissimilar) object model.
Upvotes: 2
Reputation: 146940
If you inherit from a class, then you have to use an explicit qualifier. There's no keyword.
class A {
public:
virtual bool operator==(const A&);
};
class B : public A {
public:
virtual bool operator==(const B& other) {
bool result = A::operator==(other);
//.. do other stuff
}
};
If you don't define a new one, then the base one is called.
Upvotes: 1