Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ inherit from a template using subclass

I want to create a parent class Foo inheriting from a template with argument its own nested class Sub but without get Sub out of Foo, do you have a solution ?

class Foo : public Bar<Sub>
{
    class Sub
    {
    }
}

Thanks

SOLUTION : There is no "common" way to do this so the best solution is to bring the nested class out.

like image 540
56ka Avatar asked Jul 12 '26 10:07

56ka


1 Answers

No, that's not possible directly, simply because the name Sub is not (and cannot be) in scope when you want to use it. If you cannot simply move the class out (you need it accessible as a member), you can work around it by moving the nested class out with an insanely unique name, providing a typedef for it inside the class and, if necessary, making it a friend:

class Foo_Sub_ce60eba1_67fa_48a3_b5f5_fce89d717839
{
  // Contents...
};

class Foo : Bar<Foo_Sub_ce60eba1_67fa_48a3_b5f5_fce89d717839>
{
  typedef Foo_Sub_ce60eba1_67fa_48a3_b5f5_fce89d717839 Sub;
  friend Sub;
};
like image 101
Angew is no longer proud of SO Avatar answered Jul 14 '26 23:07

Angew is no longer proud of SO



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!