S
S
sdevalex2011-12-14 16:09:10
JavaScript
sdevalex, 2011-12-14 16:09:10

C++, Abstract Factory and Strategy patterns?

Are there ready-made libraries for implementing such code?

BaseHandler::registerType<InfoHandler>(1);<br>
BaseHandler::registerType<AuthHandler>(2);<br>
<br>
BaseHandler* handler = BaseHandler::factory(1); //Находим обработчик по ID<br>


This is an attempt to shorten this code:
BaseHandler* handler = 0;<br>
<br>
switch(type)<br>
{<br>
      case 1:<br>
            handler = new InfoHandler;<br>
            break;<br>
<br>
      case 2:<br>
            handler = new AuthHandler;<br>
            break;<br>
}


When there are 3-4 such handlers, then everything is fine. And for 20 I came up with only such a detour. To solve the problem, I wrote a template class, but the linker swears at it, and did not find why ... so I'm looking for ready-made libraries.

PS In C++, isn't it possible to take out the implementation of the template class implementation from the header file? (the linker swears at this)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill, 2011-12-14
@kirill89

I have not seen such ready-made libraries, but it is impossible to take the implementation beyond the headers for sure. A template class only generates a concrete class the first time it is used. As far as I remember, if you immediately implement a specific instance in the header, then you can move the code to a cpp file, but then the meaning of the templates is lost.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question