views:

380

answers:

3

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  A: 

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.

AndreyT
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`.
Johannes Schaub - litb
A: 

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.

Sai Charan
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
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
A: 

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...

jon hanson