"Overloading" pure virtual function with different set of arguments

c++, inheritance, virtual

Solution

Why do you need a virtual method here anyway?

If mixing an `RGB` color makes only sense if the argument is another RGB color, then why should there be a generatal `mixColor(Color)` method.

If you really need it, you could override and perform a dynamic cast:

class RGB : public Color
{
public:
    void mixColors(RGB &anotherColor);
    void mixColors(Color &c) override { return mixColors(dynamic_cast<RGB&>(c)); }
};

void RGB::mixColors(RGB &kol)
{
    return RGB(0xABCDEF);
}

This way, you will get an exception at runtime if you try to mix an RGB with a color of a different class.

Problem

Consider following code sample ``` #include <iostream> using namespace std; class Color { public: virtual void mixColors(Color &anotherColor) = 0; }; class RGB : public Color { public: void mixColors(RGB &anotherColor); }; void RGB::mixColors(RGB &kol) { return RGB(0xABCDEF); } ``` I perfectly know why this code is not working (mixColors() in RGB is not implementing pure virtual function, because it has different set of arguments). However I would like to ask if is there another approach to solve this problem. Let's say I would like to mix colors, but using different algorithm for different color classes. I would appreciate any help.

Original source