Chaosteil
Chaosteil

Reputation: 160

C++ Templates and Inheritance

Let's say I have a simple Server with a template which accepts a Client as it's template argument:

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

and a Client is defined something like this:

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

But I also want say, have the class User inherit from Client (class User : public Client), so I could do Server<User> instead of Server<Client>. class User obviously needs to pass Server<Client> as a parameter when constructing Client. However, with the current implementation this seems impossible.

How should I approach this problem?

Upvotes: 2

Views: 424

Answers (1)

Dario
Dario

Reputation: 49208

What 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> {
};

Upvotes: 4

Related Questions