I would like to create a specialisation of..
template <typename CEnemy_Ty> class CEnemyManager {};
..for a class called CTank which is a CEnemy

But by defining..
template <> class CEnemyManager<CTank>{}
..it means I have to copy+paste huge chunks of unchanged code, just to change say one or two functions.

By defining..
class CTankManager: public CEnemyManager<CTank> {};
..I think I am then able to overload the functions I want to change, which is tidier.

But I don't want the class to be CTankManager. I would like it to be distinctly a CEnemyManager<CTank> since I've got other classes which deal with this template, and this would apply to the CTank class templates too. And also a lot of code is written already so I think it's better if this code didn't know about the change..

Cheers guys!

Um, just also I wanted to say it would be perfect if I could specialise a single function from a template class somehow. That's what I started off trying to do. But it seems you can only specialise an entire class. So if I could find a way of doing this through inheritance that would be cool..

Recommended Answers

All 2 Replies

put all the common stuff in a base class and inherit from it.

#include <iostream>

template< typename E > struct enemy_manager_base
{
  void same_for_all()
  { std::cout << "same for all enemies\n" ; }
};

template< typename E > struct enemy_manager : enemy_manager_base<E>
{
  void to_be_specialized()
  { std::cout << "generalization of to_be_specialized\n" ; }
};

struct tank ;

template<> struct enemy_manager<tank> : enemy_manager_base<tank>
{
  void to_be_specialized()
  { std::cout << "specialization of to_be_specialized (tank)\n" ; }
};

int main()
{
  enemy_manager<int> em_gen ;
  em_gen.same_for_all() ;
  em_gen.to_be_specialized() ;

  enemy_manager<tank> em_tank ;
  em_tank.same_for_all() ;
  em_tank.to_be_specialized() ;
}

thank you, exactly what I was looking for! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.