[英]How to separate definition and declaration of child template class
If I have a template class defined as: 如果我有一个模板类定义为:
#ifndef A_HPP
#define A_HPP
template<class T>
class A
{
public:
int doSomething(int in, bool useFirst);
private:
template<int CNT>
class B
{
public:
int doSomething(int in);
};
B<2> first;
B<3> second;
};
#include "a_imp.hpp"
#endif
Now I can go about having a declaration for A::doSomething
in an implementation header file like this 现在,我可以在这样的实现头文件中使用A::doSomething
声明
#ifndef A_IMP_HPP
#define A_IMP_HPP
template<class T>
int A<T>::doSomething(int in, bool useFirst)
{
if (useFirst)
return first.doSomething(in);
return second.doSomething(in);
}
#endif
However I do not know how I go about making the declaration for the child class's method. 但是我不知道如何为子类的方法做声明。 Is it even possible or do I have to do one of the other two ways I can think of doing this, namely defining the methods in the main header or declare the class outside of A. 是否可能或者我必须做其他两种方式中的一种我可以想到这样做,即在主标题中定义方法或在A之外声明类。
Please note that I am using C++11 so if this is only doable in that it will still work for me, though a C++98 solution would be good for other people. 请注意我正在使用C ++ 11,所以如果这只是可行的,它仍然适用于我,虽然C ++ 98解决方案对其他人有好处。
You can do it in the following way: 您可以通过以下方式执行此操作:
template <class T>
template <int CNT>
int A<T>::B<CNT>::doSomething(int in)
{
// do something
}
Not sure if this is what you're asking, but I guess you need something like this: 不确定这是不是你要问的,但我想你需要这样的东西:
template<class T>
template<int CNT>
int A<T>::B<CNT>::doSomething(int in)
{
return /*...*/;
}
Note that the template
keyword appears twice, first for the template parameters of A
, then for the parameters of nested class B
. 请注意, template
关键字出现两次,首先是A
的模板参数,然后是嵌套类B
的参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.