Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>It is required that I overload the == operator in the base class, and then override that function in the derived classes </p> </blockquote> <p>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 <code>operator==</code> 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 == <em>hiding</em> the implementation of the base class, in the few cases where someone might need to do something like that.</p> <p>Don't mix operator overloading with virtual functions -- it's painful and it's not how operator overloading is designed to operate.</p> <blockquote> <p>How do I process supers return value?</p> </blockquote> <p>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:</p> <pre><code>class SomeBaseName { int aMember; public: bool operator==(const SomeBaseName&amp; rhs) { return aMember == rhs.aMember; }; }; class Derived : public SomeBaseName { int anotherMember; public: bool operator==(const Derived&amp; rhs) //Note that this is not a virtual function, it is *hidden* instead. { if (!(SomeBaseName::operator==(rhs)) return false; return anotherMember == rhs.anotherMember; } }; </code></pre> <p>I really really recommend you not do that though.. it makes no sense to do things that way with operator overloading.</p> <blockquote> <p>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</p> </blockquote> <p>Neither will be called. The call is ambiguous and will cause a compile-time error.</p> <p>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 <strong>absolutely no equivalent in Java</strong> (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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload