C++ operator overloading and polymorphism
Asked Answered
G

4

6

Do polymorphism and operator overloading mix together?

You can't do polymorphism without pointers, as it is explained in this answer and also you can't do operator overloading with pointers as explained here. So there really isn't any way to do this, right?

Gothicize answered 29/6, 2012 at 8:43 Comment(0)
A
7

Yes there is. You didn't read the answers properly.

Here is a short demo:

#include <iostream>
using namespace std;

struct X
{
    int value;
    virtual void operator += (int x)
    {
        value += x;
    }
};

struct Y : X
{
    virtual void operator += (int x)
    {
        value *= x;
    }
};

void do_stuff(X& x)
{
    x += 10;
    cout << "Final value " << x.value << endl;
}

int main()
{
    X x;
    x.value = 10;
    Y y;
    y.value = 10;
    do_stuff(x);
    do_stuff(y);

    return 0;
}

I'm not implying that this is a good idea, or that it's practical. It's simply just possible.

Ampereturn answered 29/6, 2012 at 8:50 Comment(0)
N
4

In brief:

Operator overloading is a static polymorphism.

Static polymorphism can be implemented using function overloading, operator overloading and templates.

I think you are considering dynamic polymorphism (virtual stuff) only.
On other hand, typically we don't see virtual function for overloaded operator, but still it's possible theoritically.

A correction: Runtime (dynamic) polymorphism can be done using pointer and reference.

Narine answered 29/6, 2012 at 8:47 Comment(0)
A
3

First, polymorphism works with both references and pointers. And operator overloading works with references. So there's no problem at that level.

There is a potential problem with binary operators. Direct language support for polymorphism on an operator only works on the left hand operand. Where as for something like binary +, one logically would expect double dispatch. While this can be implemented, it is somewhat more complex, especially if the hierarchies are open.

For operators like binary +, which normally return a new object, there is also the question of the return type. Typically, this can't be a reference, since there is no object with an appropriate type to refer to. Patterns like the letter-envelop idiom have been developed to deal with this, but they aren't necessarily simple, and they often have very significant run-time overhead. Or the overloaded operator returns a special type, which only saves its arguments, and knows how to calculate the value with the correct type when requested.

Abramson answered 29/6, 2012 at 10:11 Comment(0)
M
0

You can't do polymorphism without pointers.

This is not entirely accurate. While pointers are commonly used when dealing with polymorphism, it's not a strict requirement. Polymorphism can be achieved using references and objects as well.

Also, operator overloading can indeed be performed with pointers. You can overload operators for classes that involve pointers or use pointers as parameters in overloaded operator functions.

Marcellemarcellina answered 21/12, 2023 at 10:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.