C ++ templates and inheritance

Let's say I have a simple template server that takes a client as a template argument:

template<class T>
class Server<T>{
    Server(int port);
}

      

and for the Client something like this is defined:

class Client{
    Client(Server<Client> *server, // <--
           int socket);
};

      

But I also want to say that the class User

inherits from Client

( class User : public Client

), so I could do Server<User>

instead Server<Client>

. class User

obviously needs to be passed Server<Client>

as a parameter when building Client

. However, with the current implementation, this seems impossible.

How should I approach this problem?

+2


source to share


1 answer


How about this?



template<class T>
class Server<T>{
    Server(int port);
};

template<class Derived>
class Client {
    Client(Server<Derived> *server, int socket);
    virtual ~Client() {} // Base classes should have this
};

class User : public Client<User> {
};

      

+4


source







All Articles