1

My question might not be as clear as I would like. Let me explain. I have an abstract mother class M, and a lot of child class C1,C2,...Cn. In each child, I have to define template types like this:

class Child1 : public Mother
{
    public:
    typedef AnotherTemplateClass<Child1,int>         Type1_C1;
    typedef AnotherTemplateClass<Child1,bool>        Type2_C1;
    typedef AnotherTemplateClass<Child1,unsigned>    Type3_C1;
    void DoSomething(Type1_C1 a, Type2_C1 b, Type3_C1);
};

I would like to define something like:

class Mother
{    
    public:
    typedef AnotherTemplateClass<this,int>          Type1_M;
    typedef AnotherTemplateClass<this,bool>         Type2_M;
    typedef AnotherTemplateClass<this,unsigned>     Type3_M;
};

and with Child1 using this type

class Child1 : public Mother
{
    void DoSomething(Type1_M a, Type2_M b, Type3_M c);
};

I know that is not possible to do that

error: invalid use of ‘this’ at top level

but is there any syntax that could answer this problem?

is that even possible?

Guillaume D
  • 2,202
  • 2
  • 10
  • 37

1 Answers1

2

CRTP might help:

template <typename Derived>
class ChildCrtp : public Mother
{
    public:
    typedef AnotherTemplateClass<Derived,int>         Type1_C1;
    typedef AnotherTemplateClass<Derived,bool>        Type2_C1;
    typedef AnotherTemplateClass<Derived,unsigned>    Type3_C1;

    Possibly:
    //void DoSomething(Type1_C1 a, Type2_C1 b, Type3_C1);
};

and then

class Child1 : public ChildCrtp<Child1>
{
public:
    void DoSomething(Type1_C1 a, Type2_C1 b, Type3_C1);
};
Jarod42
  • 203,559
  • 14
  • 181
  • 302