M
mati-006
Hi
I'm writing a client/server application (actually rewriting it from C).
I need three classes, one for connecting and handling this connection
(client class), one for listening (server class) and one handling the
connections for each client (server class).
I have two helper classes
(with not yet determined scope of members (private/public/protected)):
class Address {
unsigned char ip[4]; /**< IP in network byte order */
unsigned char port[2]; /**< port in network byte order */
bool ishostname;
/* this writes resolved address into ip[] and port[] */
void resolve_hostname(char *hostname, int port);
};
class Connection {
Socket socket; /**< The Socket */
bool issocket;
.... /* write,read,close functions..erm, methods ;-) */
};
I have following ideas:
- public inheritance, make methods in helper classes public and data
fields protected (to allow methods in derived classes to access them)
- composition, make methods and data fields in helper classes public,
write "dummy" methods in new classes (in order not to write Y.X.write()
but simply Y.write)
- composition, make only methods in helper classes public, but write
additional methods to access private members, and write "dummy" methods
in new classes too
Which of them (or is there any other?) is The Proper One?
I'm close to use the inheritance, because it is simplest (I think), but
similar problems described in books (I'm learning from Thinking in C++)
are resolved by composition. However, I don't have any idea how to make
it nice and proper using the composition.
I'm writing a client/server application (actually rewriting it from C).
I need three classes, one for connecting and handling this connection
(client class), one for listening (server class) and one handling the
connections for each client (server class).
I have two helper classes
(with not yet determined scope of members (private/public/protected)):
class Address {
unsigned char ip[4]; /**< IP in network byte order */
unsigned char port[2]; /**< port in network byte order */
bool ishostname;
/* this writes resolved address into ip[] and port[] */
void resolve_hostname(char *hostname, int port);
};
class Connection {
Socket socket; /**< The Socket */
bool issocket;
.... /* write,read,close functions..erm, methods ;-) */
};
I have following ideas:
- public inheritance, make methods in helper classes public and data
fields protected (to allow methods in derived classes to access them)
- composition, make methods and data fields in helper classes public,
write "dummy" methods in new classes (in order not to write Y.X.write()
but simply Y.write)
- composition, make only methods in helper classes public, but write
additional methods to access private members, and write "dummy" methods
in new classes too
Which of them (or is there any other?) is The Proper One?
I'm close to use the inheritance, because it is simplest (I think), but
similar problems described in books (I'm learning from Thinking in C++)
are resolved by composition. However, I don't have any idea how to make
it nice and proper using the composition.