简体   繁体   中英

C++: How to override method of a specific class with same interface

I have a situation where I need to inherit from two classes with same interface, but to override them separately and I definitely cannot tweak interfaces. See code example below

template<typename T>
struct Foo
{
    virtual ~Foo() = default;
    virtual void foo() = 0;
};

struct Derived : public Foo<int>, public Foo<double>
{
#if 0 // having something like this would be great, but unfortunately it doesn't work
    void Foo<int>::foo() override
    {
        std::cout << "Foo<int>::foo()" << std::endl;
    }

    void Foo<double>::foo() override
    {
        std::cout << "Foo<double>::foo()" << std::endl;
    }
#endif
};

You can always define intermediate classes that declare their own interfaces:

template<typename T>
struct Foo
{
    virtual ~Foo() = default;
    virtual void foo() = 0;
};

struct ProxyFooInt : public Foo<int>
{
    virtual void fooInt() = 0;
    
    void foo() override
    {
        return fooInt();
    }
};

struct ProxyFooDouble : public Foo<double>
{
    virtual void fooDouble() = 0;
    
    void foo() override
    {
        return fooDouble();
    }
};

struct Derived : public ProxyFooInt, public ProxyFooDouble
{
    void fooInt() override
    {
        std::cout << "Foo<int>::foo()" << std::endl;
    }

    void fooDouble() override
    {
        std::cout << "Foo<double>::foo()" << std::endl;
    }
};

A more advanced solution would be to use CRTP:

template<typename D>
struct CrtpFooInt : public Foo<int>
{
    void foo() override
    {
        return static_cast<D*>(this)->fooInt();
    }
};

template<typename D>
struct CrtpFooDouble : public Foo<double>
{
    void foo() override
    {
        return static_cast<D*>(this)->fooDouble();
    }
};

struct Derived : public CrtpFooInt<Derived>, public CrtpFooDouble<Derived>
{
    void fooInt()
    {
        std::cout << "Foo<int>::foo()" << std::endl;
    }

    void fooDouble()
    {
        std::cout << "Foo<double>::foo()" << std::endl;
    }
};

Another creative way to solve this problem using CRTP would be to use an ability to add some overloading to distinguish between different invocations of foo() :


template<typename T>
struct Foo
{
    virtual ~Foo() = default;
    virtual void foo() = 0;
};

template<typename T>
struct Tag{};

template<typename T>
struct Crtp : public Foo<T>
{
    virtual void foo(Tag<T>) = 0;

    void foo() override
    {
        foo(Tag<T>{});
    }
};

struct Derived : public Crtp<int>, public Crtp<double>
{
    void foo(Tag<int>) override
    {
        std::cout << "Foo<int>::foo()" << std::endl;
    }

    void foo(Tag<double>) override
    {
        std::cout << "Foo<double>::foo()" << std::endl;
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM