How to properly declare a self-referencing template type?

前端 未结 4 1866
误落风尘
误落风尘 2021-01-19 02:17

How do I declare a templated type that refers to itself?

template  class Animal
{
public:
    T getChild ();
}

With

4条回答
  •  遥遥无期
    2021-01-19 02:20

    In this case, you need to specify some type parameter to Animal in your typename definition, or else it would be an "infinite recursion" in the type construction:

    template class Animal;//you'll need this forward declaration
    
    template  > class Animal //int is just an example
    {
    public:
        T getPrey ();
    }
    

提交回复
热议问题