virtual operator overloading c++

10,717

Solution 1

If you want to override a virtual function in a child-class, then you need to declare the function override in the child class.

So yes, the declaration is needed.


Think about it this way: The class declaration could be used in many places and many source files, how else would the compiler know that the function has been overridden?

Solution 2

Function signatures in C++ method overriding have to match exactly (except for the return type if the return type is a pointer):

         class A { ... };
         class B : A { ... };
class A: virtual bool operator==(const A &ref) = 0;
class B:         bool operator==(const A &ref) override; // OK
class B:         bool operator==(const B &ref) override; // Invalid

If class B derived from A isn't overriding a method declared in A as virtual T foo() = 0 then class B is an abstract class.

See also the following terms:

  • covariance (computer science)
  • contravariance (computer science)

Solution 3

As stated in previous answers, you have to define the function in the derived class. Also when overriding, one should always use the keyword: override.

In your example,

virtual bool operator==(const A &ref) = 0;

is not overriden by

bool operator==(const B &ref);

Even if you define the latter, the class B will still be abstract. If the operator== in B is declared as

bool operator==(const B &ref) override;

then the compiler will produce an error informing us that this function does not override anything.

Share:
10,717
Karim O.
Author by

Karim O.

Updated on June 04, 2022

Comments

  • Karim O.
    Karim O. almost 2 years

    Suppose I wanted to overload the "==" operator for a derived class, do I need to rewrite the overload in the derived class header file or is there a way to implement the operator overload in the .cpp file without having to add anything in the header file? And if so, how would the implementation of the derived operator look like in the .cpp?

    What my header looks like:

    class A
    {
        public:
        A();
        ~A();
        virtual bool operator==(const A &ref) = 0;
        protected:
        int year;
        string note;
    }
    class B:A
    {
        public:
        B();
        ~B();
        bool operator==(const B &ref); //is this needed or not?
        private:
    
        int month, day;
    }
    
  • Karim O.
    Karim O. almost 9 years
    Thanks for reminding me to add my missing const to the declaration.
  • Vlad from Moscow
    Vlad from Moscow almost 9 years
    @Karim O. No problem.:)
  • mzoll
    mzoll about 3 years
    I get the problem arising here with abstract classes; but then how would one declare abstract Base-classes which's operators need to be overridden/implemented and thus overloaded accordingly, e.g. for operating on rhs 'other' of same class (here derived class B)? [without creating superfluous symbols for the forcefully to be implemented operators on the Base-class [B::operator==(const A& ref)], which are most likely never to be used]?
  • atomsymbol
    atomsymbol about 3 years
    @mzoll I think there are multiple points related to what you are asking about. First, C++ doesn't natively support multiple dispatch so it needs to be emulated (en.wikipedia.org/wiki/Multiple_dispatch#C++). Secondly, it is possible to define and call a non-virtual operator== even if a virtual operator== exists in the super-class (see pastebin.com/spJnse1M). Thirdly, a C++ compiler never has a full picture of the class hierarchy - any class not marked "final" must be implicitly assumed to potentially have any number of additional sub-classes that the compiler doesn't know about.