0

Suppose I have the following classes:

struct baseA {
    virtual int foo() {return 0;}
};
struct baseB {
    virtual int bar() {return 1;}
};

struct derived : public baseA, public baseB {
     typedef std::tuple<baseA, baseB> base_types;    
     virtual int buzz() {return 2;}
};

And I want to get the base classes of derived by template, I can use code like this:

template <typename T>
struct basetraits{
       typedef T::base_types types;
};

std::tuple_element<0,basetraits<derived>::types>::type 
std::tuple_element<1,basetraits<derived>::types>::type 

If I do not define base_types in the derived class or use some macro, is it possible to get the baseA and baseB by other methods?

whitebob
  • 48
  • 6
  • Just *upcast* an object of `derived`? What is the *real* problem you want to solve? Why do you need the base classes? – Some programmer dude May 07 '18 at 09:03
  • 1
    @Someprogrammerdude, Well, it is somehow complex to explain. I want to apply a template as a "decorator" to `derived` class as well as the `base` classes. However, after apply the "decorator" template, the instantiate class will not be subclass of the decorated base classes, so I can not apply the "decorator" directly to `derived` class but make the decorated `derived` class also derive from decorated `base` classes. In such situation, since i want this work being done by template, i need the base type extract to be automatic instead of being explicitly coded . – whitebob May 07 '18 at 09:13
  • https://stackoverflow.com/a/16262354/4181011 – Simon Kraemer May 07 '18 at 09:19
  • Related to: [type-trait-to-identify-primary-base-class](https://stackoverflow.com/questions/15144481/type-trait-to-identify-primary-base-class) – Jarod42 May 07 '18 at 09:20
  • @Jarod42, Thanks for the link. I think tr2::direct_base is indeed what i want. What a pity that the N2965 was not supported by the standard. – whitebob May 07 '18 at 14:38
  • @SimonKraemer, Thanks for the helpful link! this helps a lot! – whitebob May 07 '18 at 14:41

0 Answers0