0

I've been reading a lot of info, but templates are really too new for me at the moment, and I can't find any examples similar to what I'm trying to do.

Basically I have a template container defined like this:

template <class CMissile_Type>
class CMissileManager
{
std::vector<CMissile_Type> missiles;
};

The idea is that I can create different containers of arbitrary missile types.
e.g. CMissileManager<CMaverick> mavericks;
CMissileManager<CSidewinder> sidewinders;

But now I would like to create a static function which takes two arbitrary missile types and checks for collisions between them. Ideally it should be called something like this:
CMissileManager::HandleCollisions<CMaverick,CSidewinder>(mavericks,sidewinders);

so inside the CMissileManager class I defined it like this:
template <class CMissile_Ty1, class CMissile_Ty2>
static void HandleCollisions(CMissileManager<CMissile_Ty1>& m1, CMissileManager<CMissile_Ty2>& m2)
{
// stuff
}

but understandably it's still asking me for the original template argument when I call the function, i.e. it expects something like this:
CMissileManager<CMaverick>::HandleCollisions<CMaverick,CSidewinder>(mavericks,sidewinders);
since it's part of the original 1 argument template, right?

So is there any way I can get rid of the initial argument? Either by removing the need for it altogether or by passing in an empty argument?

I've been trying to use specialisation but I don't want to create a new template class just for one function. And now it's just getting really confusing..

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by twomers
0

>> So is there any way I can get rid of the initial argument?
You could just make it a friend function ...

template< typename mis_type >
  class missle_man {
  protected:
    std::vector<mis_type> vms;

  public:
    
    template< typename mis_a, 
              typename mis_b > 
      friend bool is_coll( const mis_a &missle_a,
                           const mis_b &missle_b );
    // Friend so it can access private members
  };

template< typename mis_a, 
          typename mis_b >
  bool is_coll( const mis_a &missle_a,
                const mis_b &missle_b ) {
    return missle_a.vms.size() != missle_b.vms.size();
  }


int main( void ) {
  missle_man<int> cai;
  missle_man<double> cad;

  std::cout<< is_coll(cai, cad);

  return 0;
}

I know this kind of breaks the OO scheme of your code... but it sure does make it 'look' nicer.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.