2

What's the proper way to inherit from a template class with the template argument being a nested class within the inheriting class?

class SomeClass : public TemplateClass<NestedClass>
{
     class NestedClass {};
};

3 Answers 3

4

There's no way to do specifically that. If you really have to inherit from TemplateClass<NestedClass>, you'll have to move the NestedClass declaration out of SomeClass.

2
  • 1
    Yeah can't think of another way either. You could do struct Hull { class NestedClass { }; }; class SomeClass : Hull, TemplateClass<Hull::NestedClass> { }; then, you have the nested class as SomeClass::NestedClass. Nov 11, 2009 at 0:49
  • @JohannesSchaub-litb that is a good approach for some kind of things !
    – Ghita
    Mar 26, 2015 at 15:05
0

I can't see how you could do this properly. There is this:

template<class T> class TemplateClass {};

class NestedClass {};

class SomeClass : public TemplateClass<NestedClass>
{
    typedef NestedClass NestedClass;
};

but that's just faking it...

-1

You must atleast forward declare the NestedClass:

class NestedClass;
template<class T> class TemplateClass{};
class SomeClass : public TemplateClass<NestedClass>
{
     class NestedClass{};
};

This works. Tested on MinGW c++ on windows.

Update: @jon I tried the following on with gcc version 3.4.5 on Windows XP:

#include <iostream>

class NestedClass;
template<class T> class TemplateClass{};
class SomeClass : public TemplateClass<NestedClass>
{
public:
     class NestedClass{ 
     public: 
        int a; 
        static int test(){ return 100; }
     };
     NestedClass nc;
};

int main(){
 SomeClass sc;
 sc.nc.a = 10;
 std::cout<< sc.nc.a << std::endl;
 std::cout<< sc.nc.test() << std::endl;
}

And, I get the following output: 10 100

But, I guess what the author intended (like @jon suggested) is actually this:

class SomeClass::NestedClass;
template<class T> class TemplateClass{};
class SomeClass : public TemplateClass<SomeClass::NestedClass>
{
public:
     class NestedClass{};
     NestedClass nc;
};

And this does not work. The reason being that to be able to declare SomeClass::NestedClass in the template specification, SomeClass should have been declared. But, we are trying to do exactly that - hence we get a cyclic dependency.

So I guess @jon's answer best solves this problem.

2
  • Unfortunately those are two different NestedClasses. You can check this by adding a static test() method to the nested class and then try to call it from TemplateClass. On VS2008 you get a "use of undefined type 'NestedClass'" error.
    – jon hanson
    Nov 11, 2009 at 9:01
  • Yor example doesn't call the test method of the NestedClass inside the TemplateClass. You still have two different NestedClass's - one defined inside SomeClass and a different one inside the TemplateClass base class which is declare but never defined. Anyway, as i think everyone agrees, it can't really be done.
    – jon hanson
    Nov 12, 2009 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.