I programmed a lot C in the past, not so much OOP so far. My question is about how to use objects properly, in an OOP way. First example, lets say we receive UDP messages and swap them from little to big endian. In the example below, the methods from the BinarySwapper object are more used in a functional programming way than in a OOP.
My question is if the methods of BinarySwapper should be static, because they do not have to be necessarily methods of that instance. Or is this even the right way to code OOP?
int main() {
WSASession session;
UDPSocket socket;
socket.bind(PORT);
BinarySwapper swapper;
while (true) {
char buffer[1024];
socket.recv(buffer);
swapper.swap(buffer);
// ... continue
}
}
My second question is similar. Lets take the following example. The char buffer just gets edited in the objects, at the end I send it.
int main() {
WSASession session;
UDPSocket socket;
socket.bind(PORT);
Message msg;
FormattedMessage formMsg;
while (true) {
char buffer[1024];
socket.recv(buffer);
msg.decode(buffer);
formMsg.format(buffer);
socket.send(buffer);
// ... continue
}
}
Is this a bad style? If yes, why? Is OOP more about inheritance and so on or also about to create and destroy objects "all the time"? I could also program it like this, but it look less efficient in terms of memory and runtime. Thanks for your help.
while (true) {
Message msg = socket.recv();
msg.decode();
FormattedMessage formMsg(msg);
fromMsg.format();
socket.send(formMsg);
// ... continue
}