I have a little inheritance/template problem. I'm trying to create an interface IBinding which is implemented by a TcpClient and a TcpServer (there will be 2-3 different TcpServers which differ only in the type of Stream(Socket abstraction) they generate after accepting a coection request.
Here is a simplified example:
Interface:
struct IBinding
{
virtual ~IBinding() {}
virtual void bind(const std::string endpoint) = 0;
virtual void terminate() = 0;
};
typedef std::shared_ptr<IBinding> TBindingPtr;
Header:
#include "IBinding.h"
class TcpServer : public IBinding
{
public:
TcpServer();
~TcpServer();
virtual void bind(const std::string endpoint);
virtual void terminate();
};
Implementation:
#include "TcpServer.h"
#include "StreamTypeA.h"
#include "StreamTypeB.h"
TcpServer::TcpServer() { }
TcpServer::~TcpServer() { }
void TcpServer::terminate() { }
void TcpServer::bind(const std::string endpointStr)
{
auto stream = std::make_shared<StreamTypeA>(); // Here I need to use different types depending on the server implementation
}
Now, I want to create two instances of a TcpServer and call .bind() on them, but they should create different types of Streams.
a) As far as I understand, it's not possible to pass a Type to the bind() method as an argument in c++
b) Trying to define the bind method templated also doesn't work, because it is virtual
template<class TSocket>
virtual void bind(const std::string endpoint);
c) I could probably just create two different implementations of the TcpServer
Is there another way? Isn't there a way to do it with templates?
