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 {};
};
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 {};
};
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
.
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.
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...