Zach Smith
Zach Smith

Reputation: 686

C++ class template as property type

I have a class template in a c++ project:

template<class RequestHandler = DefaultRequestHandler>
class Server { ... }

I then have another class in which I want to hold an instance of Server<WhateverRequestHandlerIWant> as a property. So currently I have something like:

class OtherClass {
public: Server<>* server;
};

Unless I am mistaken, this will only allow me to store Server classes in which the template parameter is the class DefaultRequestHandler, correct?

Is there a way to write this without just making OtherClass a class template as well?

Upvotes: 1

Views: 989

Answers (1)

juanchopanza
juanchopanza

Reputation: 227458

You could add a common abstract class for all server-like classes:

class IServer { ... };

then

template<class RequestHandler = DefaultRequestHandler>
class Server : virtual public IServer { ... }

and

class OtherClass {
public: IServer* server;
};

Upvotes: 4

Related Questions