title: Pass class name as a parameter in C++
body: <p>I have been trying to pass a class name as a parameter to a function in C++. i have a parent class 'troop' which has 10 child classes. I was unable to pass the datatype through template and had to use a bunch of if else statements to create objects of:</p>
  <pre><code>                    if (troops[i].getName() == &quot;knight&quot;) {
                          if (*mana - knight::manaPrice &gt;= 0) {
                              ptrToList-&gt;push_back(std::shared_ptr&lt;knight&gt;(new knight(ptrwindow)));
                              *mana -= knight::manaPrice;
                          }
                      }
                      if (troops[i].getName() == &quot;archer&quot;) {
                          if (*this-&gt;mana - archer::manaPrice &gt;= 0) {
                              ptrToList-&gt;push_back(std::shared_ptr&lt;archer&gt;(new archer(ptrwindow)));
                              *this-&gt;mana -= archer::manaPrice;
                          }
                      }
                      if (troops[i].getName() == &quot;wizard&quot;) {
                          if (*this-&gt;mana - wizard::manaPrice &gt;= 0) {
                              ptrToList-&gt;push_back(std::shared_ptr&lt;wizard&gt;(new wizard(ptrwindow)));
                              *this-&gt;mana -= wizard::manaPrice;
                          }
                      }
                      if (troops[i].getName() == &quot;troll&quot;) {
                          if (*this-&gt;mana - troll::manaPrice &gt;= 0) {
                              ptrToList-&gt;push_back(std::shared_ptr&lt;troll&gt;(new troll(ptrwindow)));
                              *this-&gt;mana -= troll::manaPrice;
                          }
                      }
  </code></pre>
  <p>As you can see, this could quite easily be generalized if i could pass the class name as a parameter to a function instead.</p>