Re: returning a data member's address - how?
On Apr 11, 9:44 pm, Arne Mertz <n...@arne-mertz.de> wrote:
Greg Shaw schrieb:
I see what you mean about perhaps breaking encapsulation
here. Maybe I'm designing this the wrong way. I'll tell you
my reasons - the code on my system which deals with IP
clients/servers is written in C. The function available to
me on my system to connect from a socket accepts as
arguments:
1. a pointer to the start of the IPaddress
2. an IPaddress+port length
The IPaddress and Port variables (not the actual names I
used, but you get the idea) are held in a C struct called
Socket (for argument's sake).
I don't have the expertise or access rights to change the
function or write my own in C++, so I'm stuck with that. I
did, however, fancy at least writing my own Socket class to
hold the data members. I'm a relative newbie at this and
enjoy the practice. So anyway, I have the class written, and
it contains accessor methods to Get (but not set) the
address of the IPAddress member and the combined
IPAddress+port length. Feed them into the C function, and it
should work just as if I were using a C struct to hold the
data, is the theory (yet to be tested). However, I'm
wondering if the better course might be to just use the C
struct for the job at hand, and regard my class as a
learning exercise, if I'm breaking encapsulation by
providing the address of a data member. Ho hum.
If I understand you correctly, you wrapped the IPaddress, port
(and perhaps other data) in your Socket class. But to make a
connection to the corresponding socket, your code calls the
plain C function, giving it the pointer to your socket class'
internals.
If the class is called Socket, that makes sense. But there's
also a strong argument for having a NetworkAddress class,
containing an IPAddress and a PortAddress. (Well, maybe
defining a class for the latter is a bit overkill.) In such
cases, you do need some means of obtaining a pointer from
IPAddress which can be used to call connect.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34