Question

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.

Was it helpful?

Solution

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;
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top