How to use forward declarations for a template class in a generic pointer (in C ++)

I have a base class Basedefined in Base.h :

class Base
{ /* ... */ };

And the class template Childthat comes from Baseis defined in Child.h :

#include "Base.h"

template <class T>
class Child : public Base
{ /* ... */ };

Now I want to create some factory methods in the class Base, which should return the class std::shared_ptrto the class Child. To avoid circular dependencies, I tried instead to use forward declaration. So Base.h now looks like this:

class Child; // new forward declaration

class Base
{
    /* ... */

    // new factory method
    static std::shared_ptr<Base> CreateChildInt(int i = 0)
    {
        return std::make_shared<Child<int>>(i);
    }
};

However, the definition CreateChildInt()results in the following compiler error:

"C2947: expected '>' to complete the argument template list found by '<'"

, ?
, / ?

: , factory Base Child, . factory Child, factory :

std::shared_ptr<Base> p = Child<int>::CreateChildInt(3);

<int> , :

std::shared_ptr<Base> p = Base::CreateChildInt(3);
+4
1

-, , Child, , . :

template <class T>
class Child;

. CreateChildInt::CreateChildInt Child, . Child Base , , .

: Child, Base, Base::CreateChildInt inline, Child , , Base::CreateChildInt.


PS. , . .

+3

Source: https://habr.com/ru/post/1662789/


All Articles