virtual operator overloading c++
Asked Answered
U

4

8

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;
}
Unmoved answered 4/6, 2015 at 8:23 Comment(0)
R
8

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?

Reductase answered 4/6, 2015 at 8:26 Comment(0)
P
10

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)
Prairial answered 6/10, 2020 at 13:31 Comment(2)
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]?Middleaged
@Middleaged 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.Prairial
R
8

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?

Reductase answered 4/6, 2015 at 8:26 Comment(0)
B
2

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.

Bottle answered 21/4, 2020 at 18:41 Comment(0)
G
1

The function has to be redeclared in the derived class. Otherwise 1) the derived class will be also abstract and 2) you may not define a member function of a class if it was not at first declared in this class.

Take into account that the function declaration should look like

virtual bool operator==(const A &ref) const = 0;
                                      ^^^^^ 
Glauconite answered 4/6, 2015 at 8:30 Comment(2)
Thanks for reminding me to add my missing const to the declaration.Unmoved
@Karim O. No problem.:)Glauconite

© 2022 - 2024 — McMap. All rights reserved.