tags:

views:

158

answers:

3

I have a class method with a signature like this:

// someheader.h
class Blah {
...
  void DoSomeWork(class Screen& p);
..
};

The Screen class however is supposed to turn into a template now, something like...

template <int width, int height>
class Screen {
...

So my question is, how should I change the method's prototype in someheader.h?

+6  A: 

Screen isn't a defined type, there is only a Screen<int,int>.
As you are expecting a templated type as a parameter you need to make it a function template. With that you can name the parameter type:

class Blah
{
public:
    template<int width, int height>
    void DoSomeWork(Screen<width,height>& p);
};
Georg Fritzsche
You were first :P I'd add a little tid-bit like mine had one why exactly you need the template.
GMan
Agreed, added...
Georg Fritzsche
A: 

Sometimes it's easiest to just define template functions to deal with parameters that are templatized types. Of course, this means that the implementations of those functions need to move to the header files, which can sometimes be a problem.

One alternative is to define an abstract base class and derive the template class from it, then use a pointer or reference to the abstract interface in function signatures.

class IScreenBase
{
     virtual void DoSomeWork() = 0;
};


class Blah
{
     DoSomeWork(IScreenBase& s) { s.DoSomeWork(); }
};

template <typename T>
class Screen : public IScreenBase
{
     virtual void DoSomeWork() { ... }
};

Screen<Foo> s;

Blah blah;
blah.DoSomeWork(s);
Tim Sylvester
A: 

As DoSomeWork already existed before Screen was a template, it probably does not need to know that Screen is now a template. You could thus have a ScreenBase class that defines the API that DoSomeWork needs, and Screen inherits from this:

class ScreenBase { ... };

class Blah {
   DoSomeWork(const ScreenBase& s) { ... }
};

template <int width, int height>
class Screen : public ScreenBase
{
   ...
};
Xavier Nodet