0

Let's say we have base class Table

template <typename T>
class Table
{
public:
  Table();
  virtual ~Table() = default;
private:
// get all column names as list
virtual std::list<std::string> getAllColumnsImpl();
};

and I want inherit TestTable class and override method getAllColumnsImpl from base class:

class TestTable :
public Table<TestTable>
{
public:
 TestTable();
 virtual ~TestTable() = default;

std::string Description;
int Count;


private:
// get all column names as list
std::list<std::string> getAllColumnsImpl() override;
};

Is it possible in general ?

For example I have linker errors like:

error LNK2019: unresolved external symbol "public: __cdecl Table<class TestTable>::Table<class TestTable>(void)" (??0?$Table@VTestTable@@@@QEAA@XZ) referenced in function "public: __cdecl TestTable::TestTable(void)" (??0TestTable@@QEAA@XZ)
Aleksandr Zolotov
  • 1,078
  • 3
  • 19
  • 32
  • You can't override private member functions, it has to be `protected`. – Some programmer dude Jun 01 '17 at 13:44
  • @Someprogrammerdude - the same error ... – Aleksandr Zolotov Jun 01 '17 at 13:48
  • @Someprogrammerdude - Jason Lang give answer, this is exactly CRTP - and my question has answer here https://stackoverflow.com/questions/4173254/what-is-the-curiously-recurring-template-pattern-crtp - but I didn't know about CRPT before - because can't find infomation about - may be my question can help people like me ... – Aleksandr Zolotov Jun 01 '17 at 14:29
  • I *know* what the problem is, and that's why I marked your question as a duplicate because it has already been answered. – Some programmer dude Jun 01 '17 at 15:01
  • 1
    @Someprogrammerdude - excuse me - but here https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file has no answer to my question, I agree that my question has answer - but set please right link ! – Aleksandr Zolotov Jun 01 '17 at 15:13
  • So your question is not about the linker error? One question per question please! – Some programmer dude Jun 01 '17 at 15:14
  • 1
    Linker error is just example, what happened and what wrong - in general question about CRTP ! – Aleksandr Zolotov Jun 01 '17 at 15:16
  • Then why did you mention it? It's irrelevant to what you ask about. Please take some time to [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). – Some programmer dude Jun 01 '17 at 15:17
  • 1
    Excuse me - but question is "Is it possible inherit from Template class C++ specialized on himself ?" - linker error on the end and just for example – Aleksandr Zolotov Jun 01 '17 at 15:20

1 Answers1

1

You can do it, it's called CRTP - the Curiously Recurring Template Parameter. It's very handy and there are many blogs and resources explaining uses for it.

The error you're getting is because you need to have the template's function bodies in the template's header file.

Each cpp file is compiled to a separate object file, and templates are resolved on a per-cpp file basis. When you put template code in a cpp file, then it's just "template < T >" and the compiler doesn't know what T is, so no code is generated (unless it's requested from the same cpp file, with an actual type and not T).

However, your other cpp file knows that it wants a "template < TestTable >", but it doesn't have access to the code that would make that work, because that's stuck in the other cpp file, which only knows the generic "template < T >". Neither cpp file is able to generate the missing code, so you get the linker error. Putting all the template code together in the header file removes the issue.

Jason Lang
  • 1,079
  • 9
  • 17
  • Thanks - I found explain for example here https://stackoverflow.com/questions/4173254/what-is-the-curiously-recurring-template-pattern-crtp – Aleksandr Zolotov Jun 01 '17 at 14:24