简体   繁体   English

如何分离子模板类的定义和声明

[英]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.

相关问题 C++ - 模板 class 中模板 function 的单独声明/定义 - C++ - Separate declaration/definition for template function in template class 如何使用库中的C ++ 11中的&#39;extern&#39;将定义与类模板的声明分开(dll,so,..) - How to separate definition from the declaration for a class template using 'extern' in C++11 in a library (dll, so,..) 将类声明与定义分开 - Separate class declaration from definition 如何将模板 function 的声明和定义与尾随返回类型分开? - How to separate declaration and definition for template function with trailing return type? 单独的类模板声明和专门化 - Separate class template declaration and specialization 如果不输入别名类型的完整声明,则无法从单独文件中的类模板定义访问类型别名 - Cannot access type alias from class template definition in separate file without typing full declaration of alias type 没有定义的类声明是否有效作为模板参数 - Is a class declaration without definition valid as template argument 构造函数定义中类声明的模板值 - Template value from class declaration in constructor definition G ++和LTO:如何将声明和定义分开? - G++ and LTO: how to separate declaration and definition? C ++ 17分离显式方法模板实例化声明和定义 - C++17 separate explicit method template instantiation declaration and definition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM