cannot access OnEvent(Button&, int)!




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class EmptyType {};

template
<
    class TList,
    template <class AtomicType, class Base> class Unit,
    class Root = EmptyType
>
class GenLinearHierarchy;

template
<
    class T1,
    class T2,
    template <class, class> class Unit,
    class Root
>
class GenLinearHierarchy<Typelist<T1, T2>, Unit, Root>
    : public Unit< T1, GenLinearHierarchy<T2, Unit, Root> >
{
};

template
<
    class T,
    template <class, class> class Unit,
    class Root
>
class GenLinearHierarchy<Typelist<T, NullType>, Unit, Root>
    : public Unit<T, Root>
{
};


using a unit of

1
2
3
4
5
6
7
template<typename T, typename Base>
class EventHandler : public Base
{
public:
    virtual void OnEvent(T& obj, int eventId)
    {}
};


The instantiation means:

1
2
3
4
5
6
7
8
9
10
class Window{};
class Button{};
class Scrollbar{};

using GeneratedHierarchy = Loki::GenLinearHierarchy<TYPELIST_3(Window, Button, Scrollbar), EventHandler >;

GeneratedHierarchy gen;

Window wnd;
gen.OnEvent(wnd, 56);


This compiles fine for OnEvent for template being Window because it is the first one but the other?

Functions in a derived class will hide functions with the same name in the base class. You can bring the base class functions into scope with a using declaration. Something like:
class EventHandler : Base { public: using Base::OnEvent; /*...*/ };
You can add a do-nothing OnEvent function to the root of the hierarchy to make it work.

Here is another approach:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
template<typename T>
class MyEventHandler
{
public:
    virtual void OnEvent(T& obj, int eventId) 
    {}
};

template <template <typename T> class TT, typename... Args> 
  struct whatever : TT<Args>... { using TT<Args>::OnEvent...; };

int main()
{
  whatever<MyEventHandler, Window, Button, Scrollbar> foo;
  
  Window w;
  Button b;
  Scrollbar s;
  
  foo.OnEvent(w, 100);
  foo.OnEvent(s, 100);
  foo.OnEvent(b, 100);
}
Last edited on
thanks!!
Topic archived. No new replies allowed.