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.

Tobibobi
  • 53
  • 6

1 Answers1

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);
Piotr Skotnicki
  • 46,953
  • 7
  • 118
  • 160
  • 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. – Tobibobi Sep 03 '15 at 10:16