1

I am really strugling with an inheritance issue when using what should be strongly typed enums - however the enums seems to be ambigious when used through inheritance.

Im using g++ (GCC) 4.7.2

enum class AE { V1 };
enum class BE { V2 };
enum class CE { V3 };

struct A { void set(AE enumval) { } };
struct B { void set(BE enumval) { } };
struct C { void set(CE enumval) { } };
struct combined : A, B, C { };

struct all_in_one {
    void set(AE enumval) { }
    void set(BE enumval) { }
    void set(CE enumval) { }
};

void function()
{
    combined inherited_obj;

    // compile errors - ambigious funtion call
    inherited_obj.set(AE::V1);
    inherited_obj.set(BE::V2);
    inherited_obj.set(CE::V3);


    all_in_one simple_obj;

    // works like a charm...
    simple_obj.set(AE::V1);
    simple_obj.set(BE::V2);
    simple_obj.set(CE::V3);
}

I cant find any information why it shouldn't work this way. Am I missing something obvious.

1 Answer 1

4

The functions must be in the same scope for overload resolution to work. You'd need to bring them to a common scope yourself:

struct combined : A, B, C
{
    using A::set;
    using B::set;
    using C::set;
};

DEMO

or explicitly specify the scope:

inherited_obj.A::set(AE::V1);
inherited_obj.B::set(BE::V2);
inherited_obj.C::set(CE::V3);
Sign up to request clarification or add additional context in comments.

1 Comment

Argh, that is hillarious - i thought i could do it more clever by templating the function classes so that i could eliminate a lot of code as they were essentially doing the same functionality just on their own datasets.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.